ios – UIApplicationLaunchOptionsRemoteNotificationKey没有获取userinfo

在我目前的项目中,我有一个推送通知.当我点击应用程序图标时,我想从启动选项对象获取收到的通知,但它总是返回nil:
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

解决方法

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

编辑:

如果您希望调用推送通知(通过后台获取,当您的应用程序未处于活动状态时),您需要让后端开发人员在推送通知中设置“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,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;
}

另外,不要忘记在“后台模式”中启用“远程通知

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...