首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 10后台App未收到推送通知

iOS 10后台App未收到推送通知
EN

Stack Overflow用户
提问于 2016-09-21 02:07:27
回答 2查看 11.1K关注 0票数 7

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

当App处于前台状态时,我可以收到通知。但是当App处于后台状态时,不会收到通知。每当应用程序进入前台状态时,才会收到通知。

我的代码是:

代码语言:javascript
复制
- (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处于后台状态时:

代码语言:javascript
复制
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

--根本不调用。

我在App功能中启用了后台模式的推送通知和远程通知。但是App仍然没有收到通知。

我提到了一些StackOverflow问题,但无法解决问题。在iOS版本10中有什么需要添加的吗?或者在我的代码中有什么错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-22 15:29:58

对于iOS 10,我们需要调用下面的两个方法。

前台状态的

代码语言:javascript
复制
- (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);
} 

用于后台状态

代码语言:javascript
复制
- (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();
}  

在此之前,我们必须在AppDelegate.h文件中添加并导入UserNotifications框架

代码语言:javascript
复制
#import <UserNotifications/UserNotifications.h>  
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>  
票数 10
EN

Stack Overflow用户

发布于 2019-01-08 20:52:31

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39600914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档