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

7

我正在使用FCM(Firebase Cloud Messaging)来发送iOS的推送通知。

当应用程序处于前台状态时,我能够接收到通知。但是当应用程序处于后台状态时,就无法接收到通知。只有当应用程序回到前台状态时,才会收到通知。

我的代码如下:

- (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 {


}

当应用程序处于后台状态时:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

-- 根本没有被调用。

我已经在App Capabilities中启用了推送通知和后台远程通知。但是,应用程序仍然无法接收通知。

我参考了一些StackOverflow的问题,但是无法解决这个问题。iOS版本10中是否有任何需要添加的内容或者我的代码存在任何错误?


你是否只是在iOS 10上无法在后台接收到它,还是在所有其他版本中都无法接收到? - AL.
在iOS 10中,我们需要添加新的委托来处理推送通知。我的问题是,如果我已经有一个在App Store中的应用程序,推送通知将不起作用,对吗? - Ganesh Kumar
是的。对于iOS 10它不起作用。如果他们的iPhone上有iOS 10,它将无法工作。您需要根据iOS 10编写推送代码。 - user3182143
@user3182143,我们必须提交新更改后的应用程序。 - Ganesh Kumar
@user3182143,谢谢你的回复兄弟。 - Ganesh Kumar
显示剩余3条评论
2个回答

10

iOS 10需要调用以下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);
} 

对于BACKGROUND状态

- (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>  

我遇到了相同的问题。我按照你上面的步骤做了,但不幸的是它仍然没有起作用。你能帮我吗? - Darshan Karekar
http://stackoverflow.com/questions/43694163/how-to-get-local-notification-when-app-is-running-in-objective-c/43705956#43705956 - user3182143
你不需要调用完成处理程序吗? - Cameron E
在什么情况下需要调用它?我的意思是在应用程序的前台或后台状态下? - user3182143

0

我们已经与客户进行了许多测试,并对该主题进行了大量研究。iOS 10.x,尤其是10.3.x,在处理和显示推送方面存在问题。解决问题的唯一方法是升级到更新的iOS版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接