当App在iOS 10的后台时,推送通知不会被收到 [英] Push notification not received when App is in Background in iOS 10

查看:746
本文介绍了当App在iOS 10的后台时,推送通知不会被收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FCM(Firebase云消息传递)在iOS中发送推送通知。

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



我的代码是:

$ p $ - (void)userNotificationCenter :( (UNNotificationPresentationOptions))completionHandler {
//打印消息ID。
NSDictionary * userInfo = notification.request.content.userInfo;
NSLog(@Message ID:%@,userInfo [@gcm.message_id]);

// Pring完整的信息。
NSLog(@%@,userInfo);如果([UIApplication sharedApplication] .applicationState == UIApplicationStateInactive)


{
NSLog(@INACTIVE);
completionHandler(UNNotificationPresentationOptionAlert);

else if([UIApplication sharedApplication] .applicationState == UIApplicationStateBackground)
{
NSLog(@BACKGROUND);
completionHandler(UNNotificationPresentationOptionAlert);
}
else
{
NSLog(@FOREGROUND);
completionHandler(UNNotificationPresentationOptionAlert);
$}


$ b - (void)applicationDidEnterBackground:(UIApplication *)application {



当App处于后台状态时:

 通知
withCompletionHandler:(UNNotificationPresentationOptions)) - (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification: / code>

- 完全不会被调用。

通知以及App Capabilities中的后台模式下的远程通知。但是应用程序仍然没有收到通知。

我提到了一些StackOverflow的问题,但无法解决问题。有什么可以添加在iOS版本10或在我的代码中的任何错误?
解决方案

对于iOS 10,我们需要调用2个方法。



FOR FORCEROUND状态

 <$ c (UNNotification *)通知withCompletionHandler:(void(^)(UNNotificationPresentationOptions options))completionHandler 
{
NSLog(@Handle push from前景);
//自定义代码来处理推送,而应用程序在前台
NSLog(@%@,notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionAlert);

b


$ b

背景状态

$ b (无效)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse :( UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler

pre $ b {
NSLog(@从后台处理或关闭);
//如果你在didReceiveRemoteNotification中设置了一个成员变量,你会知道这是从关闭还是背景
NSLog(@%@,response.notification.request.content.userInfo);
completionHandler();



$ b $ p
$ b

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

  #import< UserNotifications / UserNotifications.h> 
@interface AppDelegate:UIResponder< UIApplicationDelegate,UNUserNotificationCenterDelegate>


I'm using FCM(Firebase Cloud Messaging) for sending push notifications in iOS.

I'm able to receive the notification when App is in foreground state. 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:

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

I referred to some StackOverflow questions but wasn't able to solve the issue. Is there anything to add in iOS version 10 or any mistake in my code?

解决方案

For iOS 10, we need to call the 2 methods below.

For FOREGROUND state

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

#import <UserNotifications/UserNotifications.h>  
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>  

这篇关于当App在iOS 10的后台时,推送通知不会被收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆