当前位置: 首页 > 知识库问答 >
问题:

推送通知没有收到时,应用程序在后台在iOS10

刘永望
2023-03-14

我使用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 {


}

当App处于后台状态时:

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

--根本不叫。

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

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

共有2个答案

田曜瑞
2023-03-14

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

卫飞
2023-03-14

对于iOS10,我们需要调用下面的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);
} 

背景状态

- (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();
}  

在此之前,我们必须添加用户通知框架,并在App授权. h文件中导入

#import <UserNotifications/UserNotifications.h>  
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>  
 类似资料:
  • 我正在通过GCM在我的项目中执行推送通知。我的应用程序可以在前台接收通知,但不能在后台接收。 我在方法内部收到一条消息 当应用程序在前台,但我没有得到任何调用的方法 当应用程序处于后台模式时。 我在互联网上搜索了很长时间,才知道这是我收到的有效载荷格式的问题。我收到的有效载荷看起来像 [通知:{"body":"any","title":"any title"},优先级: high,content_

  • 当应用程序在前台时不接收推送通知,但当应用程序在后台时接收我已遵循来自https://medium . com/@ ankushaggarwal/GCM-setup-for-Android-push-notifications-656 cf DD 8 adbd的FCM教程

  • 当应用程序在后台时,如何更新包含数据负载的Firebase推送通知?有没有办法在通知中指定通知id给Firebase API? 完整的项目在github https://github.com/akshatashan/FireBaseCloudMessagingDemo中

  • 我想为一个聊天应用程序实现FCM推送通知服务,我遵循Firebase文档中的步骤,当通知从Firebase控制台发送到我的设备时,我会得到通知。当我尝试使用http post to https://FCM.googleapis.com/FCM/send通过FCM将通知通过服务器端发送到设备时: 当应用程序处于活动状态并且我正在将此通知发送到我的设备时,Im在我的控制台日志中收到以下消息,所以我认为

  • 所以我在firebase消息传递中经历了一个奇怪的行为。我在我的firebase messaging FCM调用中同时发送[通知]和[数据]对象,当应用程序在后台时,我可以接收到我调用FCM请求的多个推送通知。但现在的问题是,当我在打开应用程序时收到推送通知时,如果我点击了该通知,我将不再收到任何进一步的推送通知。 注意:如果我强行停止然后重新打开,我会再次开始收到通知。 我在几个安卓版本的安卓设

  • 我在我的react native应用程序中使用react native push notification来接收远程推送通知。一切正常,当应用程序处于前台或后台时,我会收到所有通知。 但是我希望通知仅在应用程序在后台时显示。如果应用程序在前台,它不应该显示通知。 任何帮助都将不胜感激。 我的清单文件包含以下代码: