gpt4 book ai didi

ios - UIApplicationLaunchOptionsRemoteNotificationKey 没有获取用户信息

转载 作者:可可西里 更新时间:2023-11-01 03:28:01 27 4
gpt4 key购买 nike

在我当前的项目中,我有一个推送通知。当我点击应用程序图标时,我想从启动选项对象接收到通知,但它总是返回 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, 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;
}

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

关于ios - UIApplicationLaunchOptionsRemoteNotificationKey 没有获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297594/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com