Browse Source

Fix UI main thread warnings with iOS11

Sebastián Katzer 8 năm trước cách đây
mục cha
commit
15d3d3e1a7
1 tập tin đã thay đổi với 32 bổ sung6 xóa
  1. 32 6
      src/ios/APPLocalNotification.m

+ 32 - 6
src/ios/APPLocalNotification.m

@@ -27,9 +27,9 @@
 
 @interface APPLocalNotification ()
 
-@property (strong, nonatomic) UIApplication* app;
 @property (strong, nonatomic) UNUserNotificationCenter* center;
 @property (readwrite, assign) BOOL deviceready;
+@property (readwrite, assign) BOOL isActive;
 @property (readonly, nonatomic, retain) NSArray* launchDetails;
 @property (readonly, nonatomic, retain) NSMutableArray* eventQueue;
 
@@ -37,7 +37,7 @@
 
 @implementation APPLocalNotification
 
-@synthesize deviceready, eventQueue;
+@synthesize deviceready, isActive, eventQueue;
 
 #pragma mark -
 #pragma mark Interface
@@ -169,7 +169,7 @@
 {
     [self.commandDelegate runInBackground:^{
         [_center clearAllNotifications];
-        [_app setApplicationIconBadgeNumber:0];
+        [self clearApplicationIconBadgeNumber];
         [self fireEvent:@"clearall"];
         [self execCallback:command];
     }];
@@ -210,7 +210,7 @@
 {
     [self.commandDelegate runInBackground:^{
         [_center cancelAllNotifications];
-        [_app setApplicationIconBadgeNumber:0];
+        [self clearApplicationIconBadgeNumber];
         [self fireEvent:@"cancelall"];
         [self execCallback:command];
     }];
@@ -560,16 +560,43 @@
 - (void) pluginInitialize
 {
     eventQueue = [[NSMutableArray alloc] init];
-    _app       = [UIApplication sharedApplication];
     _center    = [UNUserNotificationCenter currentNotificationCenter];
 
     _center.delegate = self;
     [_center registerGeneralNotificationCategory];
+
+    [self monitorAppStateChanges];
+}
+
+/**
+ * Monitor changes of the app state and update the _isActive flag.
+ */
+- (void) monitorAppStateChanges
+{
+    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+
+    [center addObserverForName:UIApplicationDidBecomeActiveNotification
+                        object:NULL queue:[NSOperationQueue mainQueue]
+                    usingBlock:^(NSNotification *e) { isActive = YES; }];
+
+    [center addObserverForName:UIApplicationDidEnterBackgroundNotification
+                        object:NULL queue:[NSOperationQueue mainQueue]
+                    usingBlock:^(NSNotification *e) { isActive = NO; }];
 }
 
 #pragma mark -
 #pragma mark Helper
 
+/**
+ * Removes the badge number from the app icon.
+ */
+- (void) clearApplicationIconBadgeNumber
+{
+    dispatch_async(dispatch_get_main_queue(), ^{
+        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
+    });
+}
+
 /**
  * Simply invokes the callback without any parameter.
  */
@@ -625,7 +652,6 @@
       notification:(UNNotificationRequest*)request
               data:(NSMutableDictionary*)data
 {
-    BOOL isActive = [_app applicationState] == UIApplicationStateActive;
     NSString *js, *params, *notiAsJSON, *dataAsJSON;
     NSData* dataAsData;