UIApplicationLaunchOptionsRemoteNotificationKey没有获取userinfo [英] UIApplicationLaunchOptionsRemoteNotificationKey not getting userinfo

查看:220
本文介绍了UIApplicationLaunchOptionsRemoteNotificationKey没有获取userinfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的项目中,我有推送通知。当我点击应用程序图标时,我想从启动选项对象获得收到的通知,但它总是返回 nil

In my current project I have a push notification. When I tap the app icon I want to get the received notification from the launch options object, but it always returns nil:

NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];


推荐答案

你无法检测到这种情况,因为申请是没有使用推送通知打开(它已通过应用程序图标打开)。

尝试通过刷推推送通知打开应用程序。

You can't detect that case, because application is not open using push notification (it has been open via app icon).
Try to open application by swiping push notification.

编辑:

如果您想调用推送通知(通过后台获取,当您的应用程序未处于活动状态时),您需要让后端开发人员设置推送通知中的content-available:1

If you want to be invoked for push notification (via background fetch, when your application is not active) you need to ask your backend developer to set "content-available": 1 in push notification.

之后 -application: didReceiveRemoteNotification:fetchCompletionHandler:将被调用(当推送通知到达时),因此您可以将有效负载保存到文件中,然后当应用程序打开时,您可以读取文件并采取措施。

After that -application:didReceiveRemoteNotification:fetchCompletionHandler: will be invoked (when push-notification arrives), so you can save the payload into a file and then when application will be open, you can read the file and take actions.

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"#BACKGROUND FETCH CALLED: %@", userInfo);
    // When we get a push, just writing it to file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"userInfo.plist"];

    [userInfo writeToFile:filePath atomically:YES];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Checking if application was launched by tapping icon, or push notification
    if (!launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"userInfo.plist"];

        [[NSFileManager defaultManager] removeItemAtPath:filePath
                                                   error:nil];
        NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:filePath];
        if (info) {
            // Launched by tapping icon
            // ... your handling here
        }
    } else {
        NSDictionary *info = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
        // Launched with swiping
        // ... your handling here
    }
    return YES;
}

另请不要忘记在后台模式中启用远程通知

Also don't forget to enable "Remote notifications" in "Background Modes"

这篇关于UIApplicationLaunchOptionsRemoteNotificationKey没有获取userinfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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