在后台接收推送通知后,当我点击应用程序图标时,didReceiveRemoteNotification没有被调用

11
当我的应用程序处于后台且我收到远程通知时,会发生两件事情:
  1. 我点击推送通知横幅,我的应用程序进入前景,并调用didReceiveRemoteNotification方法。
  2. 我从主屏幕点击我的应用程序图标,我的应用程序进入前景,但不会调用didReceiveRemoteNotification方法。
因此,在场景1中,我可以响应didReceiveRemoteNotification更新应用程序中未读消息的计数器。 在场景2中,我无法这样做。
如何使用Quickblox解决这个问题?

1
你能解决这个问题吗? - Nameet
你解决了这个问题吗? - Anshad Rasheed
@MarioFrade 我已更新代理答案,您遇到的问题是预期行为。 - onmyway133
4个回答

3
作为一种可能的变体:
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
       [self handleRemoteNotifications:userInfo];
    }

    // Override point for customization after application launch.
    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
   [self handleRemoteNotifications:userInfo];
} 

#pragma mark - Remote notifications handling

 -(void)handleRemoteNotifications:(NSDictionary *)userInfo {
   // do your stuff
}

@end

当应用程序在后台运行且未被杀死并且我使用图标启动应用程序时,didFinishLaunchingWithOptions方法不会被调用。 - sheetal
应在这种情况下调用didReceiveRemoteNotification:函数。 - frankWhite
嗨,@sheetal,我有一个类似的场景,但需要稍作修改,比如当我收到推送通知时,应用程序应在后台激活并执行给定的任务(例如用户位置更新代码)-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo,但我的应用程序在用户点击通知之前不会被激活或调用。那么,在应用程序处于后台时,我该如何完成此操作?有人知道解决方案吗? - Moxarth
你好@Moxarth,抱歉回复晚了,但我认为最好创建一个单独的问题主题,因为例如我不知道它如何实现。 - frankWhite

2
问题可能是如果应用程序未运行,则不会调用application:didReceiveRemoteNotification:。引用苹果文档所述:

This document is outdated

如果应用程序在推送通知到达时未运行,则该方法将启动应用程序并在启动选项字典中提供适当的信息。应用程序不会调用此方法来处理该推送通知。相反,您需要实现application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:方法来获取推送通知有效载荷数据并采取相应措施。

这是新文档。

使用此方法处理您的应用程序的传入远程通知。与只有在应用程序在前台运行时才调用的application:didReceiveRemoteNotification:方法不同,系统在您的应用程序在前台或后台运行时调用此方法。此外,如果启用了远程通知后台模式,则当远程通知到达时,系统将启动您的应用程序(或从挂起状态唤醒它)并将其置于后台状态。但是,如果用户已经强制退出了应用程序,则系统不会自动启动您的应用程序。在这种情况下,用户必须重新启动您的应用程序或重新启动设备,然后系统才会再次尝试自动启动您的应用程序。


2
在didFinishLaunchingWithOptions方法中,您可以使用以下代码获取推送的有效负载:<code> NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; </code> - Alessio Campanelli
嗨,我有一个类似的情况,但需要稍作修改,例如当我收到推送通知时,应用程序应在后台激活并执行给定的任务(如用户位置更新代码)-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo,但我的应用程序直到用户点击通知才会激活。那么,在应用程序处于后台时,我该如何完成此操作?有人知道解决方案吗? - Moxarth
我的应用在后台状态下收到通知时,无法自动唤醒或激活。我们必须点击通知才能使其激活。 - Moxarth

2

当应用未运行时,在didFinishLaunchingWithOptions方法中,您可以使用以下代码获取推送的有效载荷:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

NSString *myKey = [userInfo objectForKey:@"myKeyFromPayload"];
    }

请记得在 plist 中设置权限。

对于远程推送,您可以在您的 appdelegate 中使用以下代码:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

1

您需要在后台模式中启用远程通知。

自动执行此操作的方法:(Xcode5)

- Go to your Project settings -> Capabilities -> Background Modes
- Tick "Remote Notifications"

要手动执行此操作:
- Open your %appname%-Info.plist
- Right click and tick "Show Raw Keys/Values"
- Right click and choose "Add Row"
- Type in "UIBackgroundModes" (Key)
- The key will be created, and the type is an Array
- Add new item in the array with the value of "remote-notification" (Value) and press enter
- Now you have 1 item in your array called: "Item 0", if you had any other items in there, just add this item (remote-notification) to the array.

请使用FrankWhite使用的这些方法 :)
希望能对你有所帮助 ;)

5
我有类似的问题。这并没有解决这个问题。 - Nameet
从文档中得知:“使用推送通知来通知用户新内容可用的应用程序可以在后台获取内容。为支持此模式,请在应用程序的Info.plist文件中包含带有remote-notification值的UIBackgroundModes键。您还必须在应用程序委托中实现application:didReceiveRemoteNotification:fetchCompletionHandler:方法。”因此,我认为这并不是一般相关的。 - mxcl

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