didReceiveRemoteNotification在我在后台收到推送通知后点击应用程序图标时未被调用

当我的应用程序在后台并收到远程通知时,可能会发生两件事情:

  1. 我点击推送通知横幅,我的应用程序来到前台,didReceiveRemoteNotification被调用。

  2. 我点击跳板上的应用程序图标,我的应用程序进入前台,didReceiveRemoteNotification不被调用。

因此,在场景1中,我可以通过didReceiveRemoteNotification来更新应用内未读消息的计数器。 在情况2中,我不能。

我怎样才能解决这个使用QuickBlox?

作为一个可能的变体:

@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中:您可以使用此代码获取推送的有效内容:

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

问题可能是application:didReceiveRemoteNotification:如果应用程序没有运行,则不会被调用。 引用Apple文档:

这个文件已经过时了

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

这是新的文件

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

您必须在后台模式下启用远程通知。

为此,自动:(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使用:)

希望这可以帮助 ;)