简体   繁体   English

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

[英]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.

相关问题 当应用处于后台模式或被强制停止iOS 10时未收到推送通知 - Push Notification is not received when the app is in background mode or it is force stopped iOS 10 当应用程序在iOS 10的后台运行时无法访问推送通知 - Unable to access push notification when app is in background for iOS 10 当应用程序从后台收到推送通知或在iOS中终止时,无法收听来自NSNotificationCenter的通知 - Unable to listen notification from NSNotificationCenter when App received push notification from background or terminated in iOS 当收到静默推送通知并且应用程序在后台,iOS中运行时,执行几行代码 - Execute few lines of code when received silent push notification and app is running in background, ios 收到推送通知时在后台执行代码 iOS15 - Execute code in the background when push notification is received iOS15 iOS应用程序在后台时的GCM推送通知 - GCM push notification when iOS app is in the background 迅速3,iOS 10-未收到推送通知Firebase - swift 3, ios 10 - push notification firebase is not received 应用程序处于前台时未收到推送通知 ios flutter - Push Notification not received when app is in foreground ios flutter 应用在前台运行时收到 iOS 推送通知 - iOS push notification received when app is running in foreground Worklight-在iOS中后台处理收到的推送通知 - Worklight - Processing a received push notification in the background in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM