gpt4 book ai didi

ios - 关于在ios中保存来自远程通知的信息

转载 作者:行者123 更新时间:2023-11-29 10:38:42 25 4
gpt4 key购买 nike

我有一个带有结构的推送通知

{
"aps":
{
"alert": "Hello, world!",
"sound": "default",
"funcId": "abc" <-- this key i add more
}
}

我想存储键 "funcId" 的值,以便在应用收到通知(应用处于背景状态或被杀死)后使用,例如:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSString *sFuncID = [[userInfo objectForKey:@"aps"] objectForKey:@"funcId"];
[[NSUserDefaults standardUserDefaults] setValue:sFuncID forKey:Key_ID_notification];
[[NSUserDefaults standardUserDefaults] synchronize];
}

我曾尝试使用 NSUserDefaults 来存储这个值,但是当应用程序处于状态后台并杀死这个值时,这个值没有存储。如何存储远程通知的值,以便在应用重启时使用?

感谢大家的支持!

最佳答案

您必须使用两种不同的方法处理推送通知,因此,您必须使用两种不同的方法保存该值。在将值/对象保存到 NSUserDefaults 之前,您还需要确保 userInfo 不是 nil

代码如下:-

//Receive Push Notification when the app is active in foreground or background
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {

if(userInfo){
//TODO: Handle the userInfo here
NSString *sFuncID = [[userInfo objectForKey:@"aps"] objectForKey:@"funcId"];
[[NSUserDefaults standardUserDefaults] setValue:sFuncID forKey:Key_ID_notification];
[[NSUserDefaults standardUserDefaults] synchronize];
}

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Get the push notification when app is not open
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

if(remoteNotif){
[self handleRemoteNotification:application userInfo:remoteNotif];
}

return YES;
}

-(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{

if(userInfo){
//TODO: Handle the userInfo here
NSString *sFuncID = [[userInfo objectForKey:@"aps"] objectForKey:@"funcId"];
[[NSUserDefaults standardUserDefaults] setValue:sFuncID forKey:Key_ID_notification];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}

关于ios - 关于在ios中保存来自远程通知的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25619259/

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