iOS 5使用NSDictionary从发送的推送通知中检索信息

Nic*_*ick 3 iphone nsdictionary apple-push-notifications ios5

是否可以使用NSDictionary从发送的推送通知中检索信息?(例如,获取警报有效负载包含的标题,消息和声音).

我还想在有效负载中发送信息(例如字符串),供应用程序使用与标题或消息无关的信息.再说一遍,这可能吗?

til*_*ilo 8

是的,两者都有可能!

请参阅获取所需信息,执行以下操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Push notification was received when the app was in the background

    // ..... 
    if (launchOptions != nil)
    {
        NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
            NSLog(@"Launched from push notification: %@", dictionary);
            // do something with your dictionary
        }
    }
    // ..... 
    return YES;
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    //  Push notification received while the app is running

    NSLog(@"Received notification: %@", userInfo);
    // do something with your dictionary
}
Run Code Online (Sandbox Code Playgroud)