【问题标题】:UIApplicationLaunchOptionsRemoteNotificationKey not getting userinfoUIApplicationLaunchOptionsRemoteNotificationKey 未获取用户信息
【发布时间】:2015-07-29 15:02:17
【问题描述】:

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

NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

【问题讨论】:

  • 你能显示整个代码或你尝试通过哪种方法获取用户信息吗?
  • in "- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}" 和 "- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)启动选项”

标签: ios objective-c


【解决方案1】:

您无法检测到这种情况,因为应用程序未使用推送通知打开(它已通过应用程序图标打开)。
尝试通过滑动推送通知打开应用程序。

编辑:

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

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

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

别忘了在“后台模式”中启用“远程通知”

【讨论】:

  • 通过滑动推送通知打开应用程序正在工作,但我也希望在应用程序图标上点击同样的东西
  • @l0gg3r 在启用远程通知后台模式后在 ios8 设备中也面临同样的问题..
  • @JaydeepChauhan 你是对的,字典将是 nil,但是当推送通知到达时你会被调用,所以你可以将有效负载保存到文件中,当用户打开你的应用程序时读取文件并删除它。
  • 感谢您的快速回复。您能给我任何演示,以便我理解。
  • 当应用处于非活动状态时,通知未在 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 中接收。我有做了与上述相同的事情。请建议/指导我。
【解决方案2】:

当您从 PUSH NOTIFICATION ACTION 启动应用程序时,[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] 将向您返回推送通知负载(字典格式)。当我说推送通知操作时,它意味着从操作中心或从推送通知警报对话框中点击推送通知(根据设备设置,推送通知传递机制会有所不同)。

如果您通过点击 APP 图标启动应用程序,[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] 始终返回 nil。因为,它还没有从任何类型的推送通知中启动。

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 2011-03-13
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2021-02-18
    • 2018-03-26
    相关资源
    最近更新 更多