فهرست منبع

Fully migrate id to int and warn when NaN

Sebastián Katzer 10 سال پیش
والد
کامیت
30f8f6aa8e

+ 1 - 1
src/android/notification/Builder.java

@@ -158,7 +158,7 @@ public class Builder {
             return;
 
         Intent deleteIntent = new Intent(context, clearReceiver)
-                .setAction(options.getId())
+                .setAction(options.getIdStr())
                 .putExtra(Options.EXTRA, options.toString());
 
         PendingIntent dpi = PendingIntent.getBroadcast(

+ 7 - 7
src/android/notification/Notification.java

@@ -105,7 +105,7 @@ public class Notification {
      * Get notification ID.
      */
     public int getId () {
-        return options.getIdAsInt();
+        return options.getId();
     }
 
     /**
@@ -168,7 +168,7 @@ public class Notification {
 
         // Intent gets called when the Notification gets fired
         Intent intent = new Intent(context, receiver)
-                .setAction(options.getId())
+                .setAction(options.getIdStr())
                 .putExtra(Options.EXTRA, options.toString());
 
         PendingIntent pi = PendingIntent.getBroadcast(
@@ -208,13 +208,13 @@ public class Notification {
      */
     public void cancel() {
         Intent intent = new Intent(context, receiver)
-                .setAction(options.getId());
+                .setAction(options.getIdStr());
 
         PendingIntent pi = PendingIntent.
                 getBroadcast(context, 0, intent, 0);
 
         getAlarmMgr().cancel(pi);
-        getNotMgr().cancel(options.getIdAsInt());
+        getNotMgr().cancel(options.getId());
 
         unpersist();
     }
@@ -232,7 +232,7 @@ public class Notification {
      */
     @SuppressWarnings("deprecation")
     private void showNotification () {
-        int id = getOptions().getIdAsInt();
+        int id = getOptions().getId();
 
         if (Build.VERSION.SDK_INT <= 15) {
             // Notification for HoneyComb to ICS
@@ -309,7 +309,7 @@ public class Notification {
     private void persist () {
         SharedPreferences.Editor editor = getPrefs().edit();
 
-        editor.putString(options.getId(), options.toString());
+        editor.putString(options.getIdStr(), options.toString());
 
         if (Build.VERSION.SDK_INT < 9) {
             editor.commit();
@@ -324,7 +324,7 @@ public class Notification {
     private void unpersist () {
         SharedPreferences.Editor editor = getPrefs().edit();
 
-        editor.remove(options.getId());
+        editor.remove(options.getIdStr());
 
         if (Build.VERSION.SDK_INT < 9) {
             editor.commit();

+ 6 - 10
src/android/notification/Options.java

@@ -203,21 +203,17 @@ public class Options {
     }
 
     /**
-     * ID for the local notification.
+     * ID for the local notification as a number.
      */
-    public String getId() {
-        return options.optString("id", "0");
+    public Integer getId() {
+        return options.optInt("id", 0);
     }
 
     /**
-     * ID for the local notification.
+     * ID for the local notification as a string.
      */
-    public int getIdAsInt() {
-        try {
-            return Integer.parseInt(getId());
-        } catch (Exception ignore) {
-            return 0;
-        }
+    public String getIdStr() {
+        return getId().toString();
     }
 
     /**

+ 5 - 5
src/ios/APPLocalNotification.m

@@ -102,7 +102,7 @@
 
     [self.commandDelegate runInBackground:^{
         for (NSDictionary* options in notifications) {
-            NSString* id = [options objectForKey:@"id"];
+            NSNumber* id = [options objectForKey:@"id"];
             UILocalNotification* notification;
 
             notification = [self.app localNotificationWithId:id];
@@ -133,7 +133,7 @@
 - (void) cancel:(CDVInvokedUrlCommand*)command
 {
     [self.commandDelegate runInBackground:^{
-        for (NSString* id in command.arguments) {
+        for (NSNumber* id in command.arguments) {
             UILocalNotification* notification;
 
             notification = [self.app localNotificationWithId:id];
@@ -170,7 +170,7 @@
 - (void) clear:(CDVInvokedUrlCommand*)command
 {
     [self.commandDelegate runInBackground:^{
-        for (NSString* id in command.arguments) {
+        for (NSNumber* id in command.arguments) {
             UILocalNotification* notification;
 
             notification = [self.app localNotificationWithId:id];
@@ -241,7 +241,7 @@
               type:(APPLocalNotificationType)type;
 {
     [self.commandDelegate runInBackground:^{
-        NSString* id = [command argumentAtIndex:0];
+        NSNumber* id = [command argumentAtIndex:0];
         BOOL exist;
 
         CDVPluginResult* result;
@@ -528,7 +528,7 @@
  */
 - (void) cancelForerunnerLocalNotification:(UILocalNotification*)notification
 {
-    NSString* id = notification.options.id;
+    NSNumber* id = notification.options.id;
     UILocalNotification* forerunner;
 
     forerunner = [self.app localNotificationWithId:id];

+ 1 - 1
src/ios/APPLocalNotificationOptions.h

@@ -25,7 +25,7 @@
 
 - (id) initWithDict:(NSDictionary*)dict;
 
-@property (readonly, getter=id) NSString* id;
+@property (readonly, getter=id) NSNumber* id;
 @property (readonly, getter=badgeNumber) NSInteger badgeNumber;
 @property (readonly, getter=alertBody) NSString* alertBody;
 @property (readonly, getter=soundName) NSString* soundName;

+ 4 - 2
src/ios/APPLocalNotificationOptions.m

@@ -59,9 +59,11 @@ NSString* const DEFAULT_SOUND = @"res://platform_default";
 /**
  * The notification's ID.
  */
-- (NSString*) id
+- (NSNumber*) id
 {
-    return [dict objectForKey:@"id"];
+    NSInteger id = [[dict objectForKey:@"id"] integerValue];
+
+    return [NSNumber numberWithInteger:id];
 }
 
 /**

+ 4 - 4
src/ios/UIApplication+APPLocalNotification.h

@@ -37,14 +37,14 @@
 - (NSArray*) localNotificationIdsByType:(APPLocalNotificationType)type;
 
 // If local notification with ID exists
-- (BOOL) localNotificationExist:(NSString*)id;
+- (BOOL) localNotificationExist:(NSNumber*)id;
 // If local notification with ID and type exists
-- (BOOL) localNotificationExist:(NSString*)id type:(APPLocalNotificationType)type;
+- (BOOL) localNotificationExist:(NSNumber*)id type:(APPLocalNotificationType)type;
 
 // Local notification by ID
-- (UILocalNotification*) localNotificationWithId:(NSString*)id;
+- (UILocalNotification*) localNotificationWithId:(NSNumber*)id;
 // Local notification by ID and type
-- (UILocalNotification*) localNotificationWithId:(NSString*)id andType:(APPLocalNotificationType)type;
+- (UILocalNotification*) localNotificationWithId:(NSNumber*)id andType:(APPLocalNotificationType)type;
 
 // Property list from all local notifications
 - (NSArray*) localNotificationOptions;

+ 7 - 7
src/ios/UIApplication+APPLocalNotification.m

@@ -159,7 +159,7 @@
  * @param id
  *      Notification ID
  */
-- (BOOL) localNotificationExist:(NSString*)id
+- (BOOL) localNotificationExist:(NSNumber*)id
 {
     return [self localNotificationWithId:id] != NULL;
 }
@@ -171,7 +171,7 @@
  * @param type
  *      Notification life cycle type
  */
-- (BOOL) localNotificationExist:(NSString*)id type:(APPLocalNotificationType)type
+- (BOOL) localNotificationExist:(NSNumber*)id type:(APPLocalNotificationType)type
 {
     return [self localNotificationWithId:id andType:type] != NULL;
 }
@@ -182,13 +182,13 @@
  * @param id
  *      Notification ID
  */
-- (UILocalNotification*) localNotificationWithId:(NSString*)id
+- (UILocalNotification*) localNotificationWithId:(NSNumber*)id
 {
     NSArray* notifications = self.localNotifications;
 
     for (UILocalNotification* notification in notifications)
     {
-        if ([notification.options.id isEqualToString:id]) {
+        if ([notification.options.id isEqualToNumber:id]) {
             return notification;
         }
     }
@@ -204,7 +204,7 @@
  * @param type
  *      Notification life cycle type
  */
-- (UILocalNotification*) localNotificationWithId:(NSString*)id andType:(APPLocalNotificationType)type
+- (UILocalNotification*) localNotificationWithId:(NSNumber*)id andType:(APPLocalNotificationType)type
 {
     UILocalNotification* notification = [self localNotificationWithId:id];
 
@@ -262,7 +262,7 @@
     UILocalNotification* notification;
     NSMutableArray* options = [[NSMutableArray alloc] init];
 
-    for (NSString* id in ids)
+    for (NSNumber* id in ids)
     {
         notification = [self localNotificationWithId:id];
 
@@ -287,7 +287,7 @@
     UILocalNotification* notification;
     NSMutableArray* options = [[NSMutableArray alloc] init];
 
-    for (NSString* id in ids)
+    for (NSNumber* id in ids)
     {
         notification = [self localNotificationWithId:id];
 

+ 6 - 12
www/local-notification-core.js

@@ -172,9 +172,7 @@ exports.cancelAll = function (callback, scope) {
  *      The scope for the callback function
  */
 exports.isPresent = function (id, callback, scope) {
-    var notId = (id || '0').toString();
-
-    this.exec('isPresent', notId, callback, scope);
+    this.exec('isPresent', id || 0, callback, scope);
 };
 
 /**
@@ -188,9 +186,7 @@ exports.isPresent = function (id, callback, scope) {
  *      The scope for the callback function
  */
 exports.isScheduled = function (id, callback, scope) {
-    var notId = (id || '0').toString();
-
-    this.exec('isScheduled', notId, callback, scope);
+    this.exec('isScheduled', id || 0, callback, scope);
 };
 
 /**
@@ -204,9 +200,7 @@ exports.isScheduled = function (id, callback, scope) {
  *      The scope for the callback function
  */
 exports.isTriggered = function (id, callback, scope) {
-    var notId = (id || '0').toString();
-
-    this.exec('isTriggered', notId, callback, scope);
+    this.exec('isTriggered', id || 0, callback, scope);
 };
 
 /**
@@ -275,7 +269,7 @@ exports.get = function () {
         scope    = args[2];
 
     if (!Array.isArray(ids)) {
-        this.exec('getSingle', ids.toString(), callback, scope);
+        this.exec('getSingle', Number(ids), callback, scope);
         return;
     }
 
@@ -323,7 +317,7 @@ exports.getScheduled = function () {
     }
 
     if (!Array.isArray(ids)) {
-        this.exec('getSingleScheduled', ids.toString(), callback, scope);
+        this.exec('getSingleScheduled', Number(ids), callback, scope);
         return;
     }
 
@@ -371,7 +365,7 @@ exports.getTriggered = function () {
     }
 
     if (!Array.isArray(ids)) {
-        this.exec('getSingleTriggered', ids.toString(), callback, scope);
+        this.exec('getSingleTriggered', Number(ids), callback, scope);
         return;
     }
 

+ 8 - 5
www/local-notification-util.js

@@ -35,7 +35,7 @@ exports._defaults = {
     title: '',
     sound: 'res://platform_default',
     badge: 0,
-    id:    "0",
+    id:    0,
     data:  undefined,
     every: undefined,
     at:    undefined
@@ -133,8 +133,9 @@ exports.convertProperties = function (options) {
     if (options.id) {
         if (isNaN(options.id)) {
             options.id = this.getDefaults().id;
+            console.warn('Id is not a number: ' + options.id);
         } else {
-            options.id = options.id.toString();
+            options.id = Number(options.id);
         }
     }
 
@@ -149,6 +150,7 @@ exports.convertProperties = function (options) {
     if (options.badge) {
         if (isNaN(options.badge)) {
             options.badge = this.getDefaults().badge;
+            console.warn('Badge number is not a number: ' + options.id);
         } else {
             options.badge = Number(options.badge);
         }
@@ -181,6 +183,7 @@ exports.convertProperties = function (options) {
  *      The new callback function
  */
 exports.createCallbackFn = function (callbackFn, scope) {
+
     if (typeof callbackFn != 'function')
         return;
 
@@ -190,17 +193,17 @@ exports.createCallbackFn = function (callbackFn, scope) {
 };
 
 /**
- * Convert the IDs to Strings.
+ * Convert the IDs to numbers.
  *
  * @param {String/Number[]} ids
  *
- * @return Array of Strings
+ * @return Array of Numbers
  */
 exports.convertIds = function (ids) {
     var convertedIds = [];
 
     for (var i = 0; i < ids.length; i++) {
-        convertedIds.push(ids[i].toString());
+        convertedIds.push(Number(ids[i]));
     }
 
     return convertedIds;