我一直在尝试在我的应用程序中处理接收通知,但它并没有真正奏效。
当我使用didReceiveLocalNotification:(UILocalNotification *)notification
. 我可以接收和使用用于进入应用程序的通知,没有任何问题
但是,此功能仅在应用程序已经运行时才会触发(活动、非活动、后台和可能暂停,但我还没有尝试过)。
现在,didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
您可以使用此函数[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]
返回 UILocalNotification。
但是,当您从非运行状态启动应用程序时,不会触发此事件。LocalNotification 然后打开应用程序,但我不能以任何方式使用它。
现在,我的问题是:我怎样才能让它工作,所以我可以在启动应用程序时接收和处理通知,从通知,当应用程序处于非运行状态时?也许我在这里做错了什么?
这是我的应用程序中的一些示例代码:
首先,didFinishLaunchingWithOptions
不幸的是,该功能不起作用。该功能[sharedLocalNotificationsInstance processNotification:notification]
永远不会启动...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
[sharedLocalNotificationsInstance checkNotifications];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil ) {
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
application.applicationIconBadgeNumber = 0;
}
return YES;
}
第二段代码:didReceiveLocalNotification 函数,完美运行:我收到通知,并且 [sharedLocalNotificationsInstance processNotification:notification] 完美运行。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// Used when the application launches from a notification
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
}