【发布时间】:2013-03-07 15:33:28
【问题描述】:
如果推送通知已发送到设备,是否可以知道 iOS 应用程序何时启动? (我想访问有效负载以从通知中检索信息)
谢谢,
【问题讨论】:
标签: ios notifications push-notification apple-push-notifications
如果推送通知已发送到设备,是否可以知道 iOS 应用程序何时启动? (我想访问有效负载以从通知中检索信息)
谢谢,
【问题讨论】:
标签: ios notifications push-notification apple-push-notifications
你应该在你的代码中添加这样的东西:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[self handleRemoteNotification:application userInfo:remoteNotif];
return YES;
}
return YES;
}
请注意,只有通过点击通知启动应用程序时,您才会收到推送通知负载。如果它是通过点击应用图标启动的,您将无法获得有效负载。
【讨论】:
有来自UIApplicationDelegate的方法,你可以从中查看是否收到通知
您可以在 AppDelegate didFinishLaunchingWithOptions 方法中看到用户是否启动了带有通知的应用程序,例如
UILocalNotification *notif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
irLog(@"Recieved Notification");
}
对于您发布的本地通知,您可以查看此方法
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
对于远程通知,你可以看看这个方法
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
【讨论】: