且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

应用程序在 iOS 10 中处于后台时未收到推送通知

更新时间:2023-11-24 19:07:28

对于iOS 10,我们需要调用下面2个方法.

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

对于 FOREGROUND 状态

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

对于背景状态

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

在此之前,我们必须添加UserNotifications框架并导入到AppDelegate.h文件中

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

#import <UserNotifications/UserNotifications.h>  
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>