iOS 5使用NSDictionary从发送的推送通知中检索信息
问题描述:
是否可以使用NSDictionary从发送的推送通知中检索信息? (例如,获取警报有效载荷包含的标题,消息和声音)。iOS 5使用NSDictionary从发送的推送通知中检索信息
我也想发送有效负载中的信息(比如一个字符串),以便使用与标题或消息无关的应用程序。再次,这可能吗?
答
是的,无论是可能的!
参照获取所期望的信息,执行下列操作:
- (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
}
答
是的,你可以得到这些信息。在userInfo NSDictionary实例的内部,在关键字aps下有一个属性(其中包含另一个NSDictionary)。这包含警报,徽章和声音键的其他属性。
传递的自定义信息将出现在userInfo NSDictionary实例中,在发送推送通知时所提供的参数下。
更多信息,请参见UIApplicationDelegate协议参考:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html