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

时间:2014-02-14 10:19:43

标签: ios objective-c push-notification chat quickblox

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

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

  2. 我从跳板上点击我的应用程序图标,我的应用程序到达前台并且没有调用didReceiveRemoteNotification。

  3. 因此,在方案1中,我可以更新应用程序内部未读消息的计数器以响应didReceiveRemoteNotification。 在方案2中,我不能。

    如何使用Quickblox解决此问题?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

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

本文档已过时

  

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

这是新文件

  

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

答案 2 :(得分:2)

当app未运行时,在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

答案 3 :(得分: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使用的这些方法:)

希望这会有所帮助;)