Push notification not received when App is in Background in iOS 10

后端 未结 2 1000
我寻月下人不归
我寻月下人不归 2020-12-31 21:38

I\'m using FCM(Firebase Cloud Messaging) for sending push notifications in iOS.

I\'m able to receive the notification when App is in foreground state. But when the A

2条回答
  •  梦毁少年i
    2020-12-31 22:05

    For iOS 10, we need to call the 2 methods below.

    For FOREGROUND state

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center  willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler  
    {  
        NSLog( @"Handle push from foreground" );  
        // custom code to handle push while app is in the foreground  
        NSLog(@"%@", notification.request.content.userInfo);
        completionHandler(UNNotificationPresentationOptionAlert);
    } 
    

    For BACKGROUND state

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler  
    {  
        NSLog( @"Handle push from background or closed" );  
        // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background  
        NSLog(@"%@", response.notification.request.content.userInfo);
        completionHandler();
    }  
    

    Before that, we must add the UserNotifications framework and import in the AppDelegate.h file

    #import   
    @interface AppDelegate : UIResponder   
    

提交回复
热议问题