DidReceiveRemoteNotification called twice

I applied Push Notification in my application.

When my application is in the foreground, my application is working fine.

But when the application is in the background or killed, mine didReceiveRemoteNotificationcalls twice.

I made a general method for handling push notifications and calling this method from didFinishLaunchingWithOptionsanddidReceiveRemoteNotification

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 application is running, the ViewController is pressed once. And when I open my application From the notification banner, then my screen is double-clicked.

I placed NSLog in customPushHandlerand I got it once when the application is in the foreground and twice when I launch it from the notification banner.

What is the problem in my code.?

+4
3

didReceiveRemoteNotification, , , . , .

 - (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];
      }
}

, .

0

, :

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

           //code for pushing new controller

 }
0

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

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

    [self customPushHandler:userInfo];

    completionHandler(UIBackgroundFetchResultNewData)
}
0
source

Source: https://habr.com/ru/post/1625288/


All Articles