![](/img/trans.png)
[英]Push Notification is not received when the app is in background mode or it is force stopped iOS 10
[英]Push notification not received when App is in Background in iOS 10
我正在使用FCM(Firebase云消息传递)在iOS中发送推送通知。
当App处于前台状态时,我能够收到通知。 但是当应用程序处于后台状态时,不会收到通知。 每当应用程序进入前台状态时,只有那时才会收到通知。
我的代码是:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
// Print message ID.
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
// Pring full message.
NSLog(@"%@", userInfo);
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( @"INACTIVE" );
completionHandler(UNNotificationPresentationOptionAlert);
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( @"BACKGROUND" );
completionHandler( UNNotificationPresentationOptionAlert );
}
else
{
NSLog( @"FOREGROUND" );
completionHandler( UNNotificationPresentationOptionAlert );
}}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
当App处于后台状态时:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
- 根本没有被召唤。
我在应用程序功能的后台模式中启用了推送通知和远程通知。 但App仍未收到通知。
我提到了一些StackOverflow问题,但无法解决问题。 在iOS版本10中有什么要添加的或者我的代码中有任何错误吗?
对于iOS 10,我们需要调用以下2种方法。
对于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
文件
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
我们与客户进行了多次测试,并对该主题进行了大量研究。 iOS 10.x,特别是10.3.x在处理和显示推送方面存在问题。 解决问题的唯一方法是升级到更新的iOS版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.