Procházet zdrojové kódy

Added new getType() method and implemented isPresent, isScheduled, isTriggered for Windows

Sebastián Katzer před 8 roky
rodič
revize
04e5ef635e

+ 2 - 10
src/ios/APPLocalNotification.h

@@ -51,12 +51,8 @@
 // Cancel all notifications
 - (void) cancelAll:(CDVInvokedUrlCommand*)command;
 
-// If a notification with an ID is present
-- (void) isPresent:(CDVInvokedUrlCommand*)command;
-// If a notification with an ID is scheduled
-- (void) isScheduled:(CDVInvokedUrlCommand*)command;
-// If a notification with an ID is triggered
-- (void) isTriggered:(CDVInvokedUrlCommand*)command;
+// Notification type
+- (void) type:(CDVInvokedUrlCommand*)command;
 
 // List of all notification IDs
 - (void) ids:(CDVInvokedUrlCommand*)command;
@@ -67,10 +63,6 @@
 
 // Notification by id
 - (void) notification:(CDVInvokedUrlCommand*)command;
-// Scheduled notification by id
-- (void) scheduledNotification:(CDVInvokedUrlCommand*)command;
-// Triggered notification by id
-- (void) triggeredNotification:(CDVInvokedUrlCommand*)command;
 
 // List of notifications by id
 - (void) notifications:(CDVInvokedUrlCommand*)command;

+ 23 - 93
src/ios/APPLocalNotification.m

@@ -242,58 +242,32 @@
 }
 
 /**
- * If a notification by ID is present.
+ * Get type of notification.
  *
- * @param [ Number ]id The ID of the notification.
+ * @param [ Int ] id The ID of the notification.
  *
  * @return [ Void ]
  */
-- (void) isPresent:(CDVInvokedUrlCommand *)command
-{
-    [self exist:command byType:NotifcationTypeAll];
-}
-
-/**
- * If a notification by ID is scheduled.
- *
- * @param [ Number ]id The ID of the notification.
- *
- * @return [ Void ]
- */
-- (void) isScheduled:(CDVInvokedUrlCommand*)command
-{
-    [self exist:command byType:NotifcationTypeScheduled];
-}
-
-/**
- * Check if a notification with an ID is triggered.
- *
- * @param [ Number ]id The ID of the notification.
- *
- * @return [ Void ]
- */
-- (void) isTriggered:(CDVInvokedUrlCommand*)command
-{
-    [self exist:command byType:NotifcationTypeTriggered];
-}
-
-/**
- * Check if a notification exists by ID and type.
- *
- * @param [ APPNotificationType ] type The type of notifications to look for.
- *
- * @return [ Void ]
- */
-- (void) exist:(CDVInvokedUrlCommand*)command
-        byType:(APPNotificationType)type;
+- (void) type:(CDVInvokedUrlCommand*)command
 {
     [self.commandDelegate runInBackground:^{
         NSNumber* id = [command argumentAtIndex:0];
-        BOOL exist   = [_center notificationExist:id type:type];
-
+        NSString* type;
+
+        switch ([_center getTypeOfNotificationWithId:id]) {
+            case NotifcationTypeScheduled:
+                type = @"scheduled";
+                break;
+            case NotifcationTypeTriggered:
+                type = @"triggered";
+                break;
+            default:
+                type = @"unknown";
+        }
+        
         CDVPluginResult* result;
         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
-                                     messageAsBool:exist];
+                                     messageAsString:type];
 
         [self.commandDelegate sendPluginResult:result
                                     callbackId:command.callbackId];
@@ -360,54 +334,17 @@
  * @return [ Void ]
  */
 - (void) notification:(CDVInvokedUrlCommand*)command
-{
-    [self notification:command byType:NotifcationTypeAll];
-}
-
-/**
- * Scheduled notification by id.
- *
- * @param [ Number ] id The id of the notification to return.
- *
- * @return [ Void ]
- */
-- (void) scheduledNotification:(CDVInvokedUrlCommand*)command
-{
-    [self notification:command byType:NotifcationTypeScheduled];
-}
-
-/**
- * Triggered notification by id.
- *
- * @param [ Number ] id The id of the notification to return.
- *
- * @return [ Void ]
- */
-- (void) triggeredNotification:(CDVInvokedUrlCommand*)command
-{
-    [self notification:command byType:NotifcationTypeTriggered];
-}
-
-/**
- * Notification by type and id.
- *
- * @param [ APPNotificationType ] type The type of notifications to look for.
- *
- * @return [ Void ]
- */
-- (void) notification:(CDVInvokedUrlCommand*)command
-               byType:(APPNotificationType)type;
 {
     [self.commandDelegate runInBackground:^{
         NSArray* ids = command.arguments;
 
         NSArray* notifications;
-        notifications = [_center getNotificationOptionsByType:type andId:ids];
-
+        notifications = [_center getNotificationOptionsById:ids];
+        
         CDVPluginResult* result;
         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                messageAsDictionary:[notifications firstObject]];
-
+        
         [self.commandDelegate sendPluginResult:result
                                     callbackId:command.callbackId];
     }];
@@ -450,7 +387,7 @@
 }
 
 /**
- * List of notifications by type and id.
+ * List of notifications by type or id.
  *
  * @param [ APPNotificationType ] type The type of notifications to look for.
  *
@@ -463,18 +400,11 @@
         NSArray* ids = command.arguments;
         NSArray* notifications;
 
-        if (type == NotifcationTypeAll && ids.count == 0) {
-            notifications = [_center getNotificationOptions];
-        }
-        else if (type == NotifcationTypeAll) {
+        if (ids.count > 0) {
             notifications = [_center getNotificationOptionsById:ids];
         }
-        else if (ids.count == 0) {
-            notifications = [_center getNotificationOptionsByType:type];
-        }
         else {
-            notifications = [_center getNotificationOptionsByType:type
-                                                            andId:ids];
+            notifications = [_center getNotificationOptionsByType:type];
         }
 
         CDVPluginResult* result;

+ 4 - 10
src/ios/UNUserNotificationCenter+APPLocalNotification.h

@@ -28,7 +28,8 @@ extern NSString * const kAPPGeneralCategory;
 typedef NS_ENUM(NSUInteger, APPNotificationType) {
     NotifcationTypeAll = 0,
     NotifcationTypeScheduled = 1,
-    NotifcationTypeTriggered = 2
+    NotifcationTypeTriggered = 2,
+    NotifcationTypeUnknown = 3
 };
 
 #define APPNotificationType_DEFINED
@@ -44,15 +45,10 @@ typedef NS_ENUM(NSUInteger, APPNotificationType) {
 // List of all notification IDs from given type
 - (NSArray*) getNotificationIdsByType:(APPNotificationType)type;
 
-// Find out if notification with ID exists
-- (BOOL) notificationExist:(NSNumber*)id;
-// Find out if notification with ID and type exists
-- (BOOL) notificationExist:(NSNumber*)id type:(APPNotificationType)type;
-
 // Find notification by ID
 - (UNNotificationRequest*) getNotificationWithId:(NSNumber*)id;
-// Find notification by ID and type
-- (UNNotificationRequest*) getNotificationWithId:(NSNumber*)id andType:(APPNotificationType)type;
+// Find notification type by ID
+- (APPNotificationType) getTypeOfNotificationWithId:(NSNumber*)id;
 
 // Property list from all local notifications
 - (NSArray*) getNotificationOptions;
@@ -60,8 +56,6 @@ typedef NS_ENUM(NSUInteger, APPNotificationType) {
 - (NSArray*) getNotificationOptionsById:(NSArray*)ids;
 // Property list from all local notifications with type constraint
 - (NSArray*) getNotificationOptionsByType:(APPNotificationType)type;
-// Property list from given local notifications with type constraint
-- (NSArray*) getNotificationOptionsByType:(APPNotificationType)type andId:(NSArray*)ids;
 
 // Clear specified notfication
 - (void) clearNotification:(UNNotificationRequest*)notification;

+ 24 - 55
src/ios/UNUserNotificationCenter+APPLocalNotification.m

@@ -185,29 +185,6 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
     return ids;
 }
 
-/*
- * If the notification with the specified ID does exists.
- *
- * @param id
- *      Notification ID
- */
-- (BOOL) notificationExist:(NSNumber*)id
-{
-    return [self getNotificationWithId:id] != NULL;
-}
-
-/* If the notification with specified ID and type exists.
- *
- * @param id
- *      Notification ID
- * @param type
- *      Notification life cycle type
- */
-- (BOOL) notificationExist:(NSNumber*)id type:(APPNotificationType)type
-{
-    return [self getNotificationWithId:id andType:type] != NULL;
-}
-
 /**
  * Find notification by ID.
  *
@@ -216,33 +193,38 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
  */
 - (UNNotificationRequest*) getNotificationWithId:(NSNumber*)id
 {
-    return [self getNotificationWithId:id andType:NotifcationTypeAll];
-}
-
-/*
- * Find notification by ID and type.
- *
- * @param id
- *      Notification ID
- * @param type
- *      Notification life cycle type
- */
-- (UNNotificationRequest*) getNotificationWithId:(NSNumber*)id andType:(APPNotificationType)type
-{
-    NSArray* notifications = [self getNotificationsByType:type];
+    NSArray* notifications = [self getNotifications];
 
     for (UNNotificationRequest* notification in notifications)
     {
         NSString* fid = [NSString stringWithFormat:@"%@", notification.options.id];
-
+        
         if ([fid isEqualToString:[id stringValue]]) {
             return notification;
         }
     }
-
+    
     return NULL;
 }
 
+/**
+ * Find notification type by ID
+ */
+- (APPNotificationType) getTypeOfNotificationWithId:(NSNumber*)id
+{
+    NSArray* ids = [self getNotificationIdsByType:NotifcationTypeTriggered];
+    
+    if ([ids containsObject:id])
+        return NotifcationTypeTriggered;
+
+    ids = [self getNotificationIdsByType:NotifcationTypeScheduled];
+    
+    if ([ids containsObject:id])
+        return NotifcationTypeScheduled;
+    
+    return NotifcationTypeUnknown;
+}
+
 /**
  * List of properties from all notifications.
  */
@@ -278,29 +260,16 @@ NSString * const kAPPGeneralCategory = @"GENERAL";
  */
 - (NSArray*) getNotificationOptionsById:(NSArray*)ids
 {
-    return [self getNotificationOptionsByType:NotifcationTypeAll andId:ids];
-}
-
-/**
- * List of properties from given local notifications.
- *
- * @param type
- *      Notification life cycle type
- * @param ids
- *      Notification IDs
- */
-- (NSArray*) getNotificationOptionsByType:(APPNotificationType)type andId:(NSArray*)ids
-{
-    NSArray* notifications  = [self getNotificationsByType:type];
+    NSArray* notifications  = [self getNotifications];
     NSMutableArray* options = [[NSMutableArray alloc] init];
-
+    
     for (UNNotificationRequest* notification in notifications)
     {
         if ([ids containsObject:notification.options.id]) {
             [options addObject:notification.options.userInfo];
         }
     }
-
+    
     return options;
 }
 

+ 15 - 0
src/windows/LocalNotificationProxy.js

@@ -97,6 +97,21 @@ exports.schedule = function (success, error, args) {
     success();
 };
 
+/**
+ * Get the type of notification.
+ *
+ * @param [ Function ] success Success callback
+ * @param [ Function ] error   Error callback
+ * @param [ Array ]    args    Interface arguments
+ *
+ * @return [ Void ]
+ */
+exports.type = function (success, error, args) {
+    var type = impl.type(args[0]);
+
+    success(type);
+};
+
 /**
  * List of all notification ids.
  *

+ 30 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Manager.cs

@@ -238,5 +238,35 @@ namespace LocalNotificationProxy.LocalNotification
 
             return null;
         }
+
+        /// <summary>
+        /// Gets the type (scheduled, triggered or unknown).
+        /// </summary>
+        /// <param name="id">The ID of the notification to find for.</param>
+        /// <returns>The type of the notification or unknown type.</returns>
+        public Notification.Type GetType(string id)
+        {
+            var scheduled = ToastNotifier.GetScheduledToastNotifications();
+
+            foreach (var toast in scheduled)
+            {
+                if (toast.Tag == id)
+                {
+                    return Notification.Type.Scheduled;
+                }
+            }
+
+            var triggered = ToastNotificationManager.History.GetHistory();
+
+            foreach (var toast in triggered)
+            {
+                if (toast.Tag == id)
+                {
+                    return Notification.Type.Triggered;
+                }
+            }
+
+            return Notification.Type.Unknown;
+        }
     }
 }

+ 1 - 1
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Notification.cs

@@ -46,7 +46,7 @@
 
         public enum Type
         {
-            All, Scheduled, Triggered
+            All, Scheduled, Triggered, Unknown
         }
 
         public Options Options { get; private set; }

+ 10 - 0
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotificationProxy.cs

@@ -49,6 +49,16 @@ namespace LocalNotificationProxy
             this.manager.Schedule(notifications);
         }
 
+        /// <summary>
+        /// Gets the type of the notification specified by ID.
+        /// </summary>
+        /// <param name="id">The ID of the notification to find for.</param>
+        /// <returns>The type (scheduled, triggered or unknown).</returns>
+        public string Type(int id)
+        {
+            return this.manager.GetType(id.ToString()).ToString().ToLower();
+        }
+
         /// <summary>
         /// List of all notifiation by id.
         /// </summary>

+ 16 - 7
www/local-notification-core.js

@@ -185,24 +185,33 @@ exports.cancelAll = function (callback, scope) {
  * @return [ Void ]
  */
 exports.isPresent = function (id, callback, scope) {
-    this.exec('isPresent', id, callback, scope);
+    var fn = this.createCallbackFn(callback, scope);
+
+    this.getType(id, function (type) {
+        fn(type != 'unknown');
+    });
 };
 
 /**
- * Check if a notification is scheduled.
+ * Check if a notification has a given type.
  *
  * @param [ Int ]      id       The ID of the notification.
+ * @param [ String ]   type     The type of the notification.
  * @param [ Function ] callback The function to be exec as the callback.
  * @param [ Object ]   scope    The callback function's scope.
  *
  * @return [ Void ]
  */
-exports.isScheduled = function (id, callback, scope) {
-    this.exec('isScheduled', id, callback, scope);
+exports.hasType = function (id, type, callback, scope) {
+    var fn = this.createCallbackFn(callback, scope);
+
+    this.getType(id, function (type2) {
+        fn(type == type2);
+    });
 };
 
 /**
- * Check if a notification was triggered.
+ * Get the type (triggered, scheduled) for the notification.
  *
  * @param [ Int ]      id       The ID of the notification.
  * @param [ Function ] callback The function to be exec as the callback.
@@ -210,8 +219,8 @@ exports.isScheduled = function (id, callback, scope) {
  *
  * @return [ Void ]
  */
-exports.isTriggered = function (id, callback, scope) {
-    this.exec('isTriggered', id, callback, scope);
+exports.getType = function (id, callback, scope) {
+    this.exec('type', id, callback, scope);
 };
 
 /**

+ 15 - 2
www/local-notification.js

@@ -147,7 +147,7 @@ exports.isPresent = function (id, callback, scope) {
  * @return [ Void ]
  */
 exports.isScheduled = function (id, callback, scope) {
-    this.core.isScheduled(id, callback, scope);
+    this.core.hasType(id, 'scheduled', callback, scope);
 };
 
 /**
@@ -160,7 +160,20 @@ exports.isScheduled = function (id, callback, scope) {
  * @return [ Void ]
  */
 exports.isTriggered = function (id, callback, scope) {
-    this.core.isTriggered(id, callback, scope);
+    this.core.hasType(id, 'triggered', callback, scope);
+};
+
+/**
+ * Get the type (triggered, scheduled) for the notification.
+ *
+ * @param [ Int ]      id       The ID of the notification.
+ * @param [ Function ] callback The function to be exec as the callback.
+ * @param [ Object ]   scope    The callback function's scope.
+ *
+ * @return [ Void ]
+ */
+exports.getType = function (id, callback, scope) {
+    this.core.getType(id, callback, scope);
 };
 
 /**