Преглед изворни кода

Merge pull request #1093 from spk0611/master

iOS 10 (some hot fixes)
Sebastián Katzer пре 9 година
родитељ
комит
30da4b3606
2 измењених фајлова са 57 додато и 3 уклоњено
  1. 12 3
      src/android/LocalNotification.java
  2. 45 0
      src/ios/APPLocalNotification.m

+ 12 - 3
src/android/LocalNotification.java

@@ -37,6 +37,7 @@ import org.json.JSONObject;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
+import java.lang.Exception;
 
 import de.appplant.cordova.plugin.notification.Manager;
 import de.appplant.cordova.plugin.notification.Notification;
@@ -213,10 +214,18 @@ public class LocalNotification extends CordovaPlugin {
         for (int i = 0; i < notifications.length(); i++) {
             JSONObject options = notifications.optJSONObject(i);
 
-            Notification notification =
-                    getNotificationMgr().schedule(options, TriggerReceiver.class);
+            try {
+                Notification notification =
+                        getNotificationMgr().schedule(options, TriggerReceiver.class);
 
-            fireEvent("schedule", notification);
+                fireEvent("schedule", notification);
+            }
+            catch(Exception generic) {
+                //silently ignore the exception
+                //on some samsung devices there is a known bug where a 500 alarms limit can crash the app
+                //http://developer.samsung.com/forum/board/thread/view.do?boardName=General&messageId=280286&listLines=15&startId=zzzzz%7E&searchSubId=0000000001
+                
+            }
         }
     }
 

+ 45 - 0
src/ios/APPLocalNotification.m

@@ -26,6 +26,8 @@
 #import "UIApplication+APPLocalNotification.h"
 #import "UILocalNotification+APPLocalNotification.h"
 
+ @import UserNotifications;
+
 @interface APPLocalNotification ()
 
 // Retrieves the application state
@@ -76,6 +78,8 @@
 
             notification = [[UILocalNotification alloc]
                             initWithOptions:options];
+            notification.timeZone = [NSTimeZone defaultTimeZone];
+            notification.repeatInterval = 0;
 
             [self scheduleLocalNotification:[notification copy]];
             [self fireEvent:@"schedule" notification:notification];
@@ -691,6 +695,47 @@
  */
 - (void) fireEvent:(NSString*)event notification:(UILocalNotification*)notification
 {
+    if (IsAtLeastiOSVersion(@"10.0")) {
+        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
+        content.title = [NSString localizedUserNotificationStringForKey:notification.alertTitle arguments:nil];
+        content.body = [NSString localizedUserNotificationStringForKey:notification.alertBody
+                                                         arguments:nil];
+        content.sound = [UNNotificationSound defaultSound];
+
+        
+        NSDate *fireDate = notification.fireDate;
+        if(fireDate==nil) {
+            fireDate = [NSDate date];
+        }
+         NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
+        // Extract all date components into dateComponents
+         NSDateComponents *dateComponents = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
+         | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
+                                                           fromDate:fireDate];
+         [dateComponents setTimeZone:[NSTimeZone defaultTimeZone]];
+        
+        /// 4. update application icon badge number
+        //content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
+        
+        // Deliver the notification at the fire date.
+        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:NO];
+        
+        NSString *identifier = @"DefaultNotificationIdentifier";
+        if(notification.userInfo!=nil && [notification.userInfo objectForKey:@"id"]!=nil) {
+            identifier = [notification.userInfo objectForKey:@"id"];
+        }
+        
+        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
+        
+        /// 3. schedule localNotification
+        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
+        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
+            if (!error) {
+                NSLog(@"add NotificationRequest succeeded!");
+            }
+        }];
+    } 
+
     NSString* js;
     NSString* params = [NSString stringWithFormat:
                         @"\"%@\"", self.applicationState];