当 App 在 iOS 10 中处于后台时未收到推送通知
Push notification not received when App is in Background in iOS 10
我正在使用 FCM(Firebase Cloud Messaging) 在 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 Capabilities 的后台模式下启用了推送通知和远程通知。但是App还是收不到通知
我参考了一些 Whosebug 问题,但无法解决问题。 iOS 版本 10 中是否有任何要添加的内容或我的代码中有任何错误?
对于iOS10,我们需要调用以下2个方法。
前景状态
- (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 版本。
我正在使用 FCM(Firebase Cloud Messaging) 在 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 Capabilities 的后台模式下启用了推送通知和远程通知。但是App还是收不到通知
我参考了一些 Whosebug 问题,但无法解决问题。 iOS 版本 10 中是否有任何要添加的内容或我的代码中有任何错误?
对于iOS10,我们需要调用以下2个方法。
前景状态
- (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 版本。