简体   繁体   中英

didReceiveRemoteNotification called twice

I have implemented Push Notification in my App.

When my app is in the foreground then my app is working fine.

But when the app is in the background or is killed then my didReceiveRemoteNotification called two times.

I have made a common method for handling Push notification and calling this method from didFinishLaunchingWithOptions and didReceiveRemoteNotification

Here is my Implementation:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  NSDictionary *pushDictionary = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if (pushDictionary) {
    [self customPushHandler:pushDictionary];
  }

  return YES;

}

And :

- (void)application:(UIApplication *)application
   didReceiveRemoteNotification:(NSDictionary *)userInfo
  fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {


         [self customPushHandler:userInfo];

 }

AND:

 - (void) customPushHandler:(NSDictionary *)notification {

     // Code to push ViewController

         NSLog(@"Push Notification"); //Got it two times when Clicked in notification banner

   }

When my App is running then ViewController is pushed Once. And when I open my app From notification banner then My screen is pushed twice.

I placed a NSLog in customPushHandler and I got it one time when App is in foreground and Two time when I launch it from Notification banner.

What is issue in my code.?

" When the app is in background, the method is called twice, once when you receive the push notification, and other time when you tap on that notification. "

You can verify the application state and ignore the notification received if the app is in background.

Solution: Remote notification method called twice

Swift 4 code:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        if (application.applicationState == .background) {
            completionHandler(.noData)
            return
        }

    // logic
    // call completionHandler with appropriate UIBackgroundFetchResult
}

check this one condition in didReceiveRemoteNotification , like if you tap on notification while app is in background . By that way, you can avoid getting called twice.

 - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
      UIApplicationState state = [[UIApplication sharedApplication] applicationState];
      if (state == UIApplicationStateBackground || state == UIApplicationStateInactive || state == UIApplicationStateForeground || state == UIApplicationStateActive)
      {
          //Do checking here.
          [self customPushHandler:userInfo];
      }
}

Check I have edited my answer.

Check top view controller in current navigation stack like:

if(![self.navigationController.topViewController isKindOfClass:[MyClass class]]) {

           //code for pushing new controller

 }

Whenever you use backgroundFetch for remote notification, add a completion handler for that.

- (void)application:(UIApplication *)application
   didReceiveRemoteNotification:(NSDictionary *)userInfo
   fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {

    [self customPushHandler:userInfo];

    completionHandler(UIBackgroundFetchResultNewData)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM