关于在 ios 中保存来自远程通知的信息 [英] About save information from remote notification in ios

查看:22
本文介绍了关于在 ios 中保存来自远程通知的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a push notification with struct

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

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

I want to store value of key "funcId" to use after app recives notification (app in state backgound or killed) like:

-(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 来存储这个值,但是当应用程序处于状态后台并杀死这个值时,不会存储.如何存储远程通知中的值以在应用重新启动时使用?

I had try use NSUserDefaults to store this value, but when app in state background and killed this value not store. How to store vaule from remote notification to use when app relaunch?

感谢大家的支持!

推荐答案

您将不得不以 2 种不同的方法处理推送通知,因此,您将不得不以 2 种不同的方法保存值.在将值/对象保存到 NSUserDefaults 之前,您还需要确保 userInfonot nil.

You will have to handle Push Notification in 2 different methods, so, you will have to save the value in 2 different methods. You will also need to make sure that the userInfo is not nil before saving the value/object into NSUserDefaults.

代码如下:-

//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 中保存来自远程通知的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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