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

时间:2012-01-18 13:54:56

标签: iphone ios5 nsdictionary apple-push-notifications

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

我还想在有效负载中发送信息(例如字符串),以便使用与标题或消息无关的应用。再次,这可能吗?

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:2)

是的,您可以获得此信息。在userInfo NSDictionary实例内部,在键aps下有一个属性(包含另一个NSDictionary)。这包含警报,徽章和声音键的其他属性。

传递的自定义信息将在发送推送通知时根据您提供的参数显示在userInfo NSDictionary实例中。

有关详细信息,请参阅UIApplicationDelegate协议参考:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html