应用程序在 iOS 11 中处于后台时未收到推送通知
Posted
技术标签:
【中文标题】应用程序在 iOS 11 中处于后台时未收到推送通知【英文标题】:Push notification not received when App is in Background in iOS 11 【发布时间】:2018-04-11 07:05:50 【问题描述】:我正在使用 UNUserNotificationCenter 在 ios 中发送推送通知。
当应用程序处于前台状态时,我能够收到通知。但是当App处于后台状态时,并没有收到通知。每当应用程序进入前台状态时,才会收到通知。
注册远程通知:
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0"))
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
if( !error )
dispatch_async(dispatch_get_main_queue(), ^
[[UIApplication sharedApplication] registerForRemoteNotifications];
);
else
NSLog( @"Push registration FAILED" );
NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
];
else
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
当应用处于前台模式时,调用该方法:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
但此方法在后台模式下不起作用。 我提到了一些 *** 问题,但无法解决问题。 iOS 11 版有什么要添加的吗?
【问题讨论】:
你调用什么函数来发送通知? @KakshilShah 现在检查我的问题。 Receiving Push Notifications while in background的可能重复 【参考方案1】:如果是远程通知,则在应用委托中调用此方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
//Handle notification here!
【讨论】:
确保后台模式已启用远程通知 我正在使用 UNUserNotificationCenter 委托方法来接收远程通知:- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
@Kakshil Shah 只有当通知到达并且表明有数据要获取时才会触发该委托。 developer.apple.com/documentation/uikit/uiapplicationdelegate/…以上是关于应用程序在 iOS 11 中处于后台时未收到推送通知的主要内容,如果未能解决你的问题,请参考以下文章