[英]Push notification not received when App is in Background in iOS 10
I'm using FCM(Firebase Cloud Messaging) for sending push notifications in iOS. 我正在使用FCM(Firebase云消息传递)在iOS中发送推送通知。
I'm able to receive the notification when App is in foreground state. 当App处于前台状态时,我能够收到通知。 But when the App is in background state, the notification is not received.
但是当应用程序处于后台状态时,不会收到通知。 Whenever the application will come to foreground state, only then will the notification be received.
每当应用程序进入前台状态时,只有那时才会收到通知。
My code is: 我的代码是:
- (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 {
}
When App is in background state: 当App处于后台状态时:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
-- is not called at all. - 根本没有被召唤。
I enabled push notifications and also remote notifications in background modes in App Capabilities. 我在应用程序功能的后台模式中启用了推送通知和远程通知。 But App is still not receiving the notification.
但App仍未收到通知。
I referred to some StackOverflow questions but wasn't able to solve the issue. 我提到了一些StackOverflow问题,但无法解决问题。 Is there anything to add in iOS version 10 or any mistake in my code?
在iOS版本10中有什么要添加的或者我的代码中有任何错误吗?
For iOS 10, we need to call the 2 methods below. 对于iOS 10,我们需要调用以下2种方法。
For FOREGROUND state 对于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);
}
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 在此之前,我们必须添加
UserNotifications
框架并导入AppDelegate.h
文件
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
We've run many tests with our customers and did lots of research on the topic. 我们与客户进行了多次测试,并对该主题进行了大量研究。 iOS 10.x, especially 10.3.x has issues handling and displaying pushes.
iOS 10.x,特别是10.3.x在处理和显示推送方面存在问题。 The only way to circumvent the issues is by upgrading to a newer iOS-version.
解决问题的唯一方法是升级到更新的iOS版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.