我使用FCM(Firebase Cloud Messaging)在iOS中发送推送通知。
当App处于前台状态时,我可以收到通知。但是当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功能中启用了后台模式的推送通知和远程通知。但是App仍然没有收到通知。
我提到了一些StackOverflow问题,但无法解决问题。在iOS版本10中有什么需要添加的吗?或者在我的代码中有什么错误?
发布于 2016-09-22 15:29:58
对于iOS 10,我们需要调用下面的两个方法。
前台状态的
- (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();
} 在此之前,我们必须在AppDelegate.h文件中添加并导入UserNotifications框架
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 发布于 2019-01-08 20:52:31
我们已经对我们的客户进行了许多测试,并在这个主题上做了大量的研究。iOS 10.x,尤其是10.3.x存在处理和显示推送的问题。绕过这些问题的唯一方法是升级到更新的iOS版本。
https://stackoverflow.com/questions/39600914
复制相似问题