我一直在尝试处理在我的应用程序中接收通知,但它并没有真正奏效。
当我使用 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];
}
最佳答案
这就是 iOS 处理本地通知的方式。这取决于您的应用程序的状态,例如事件、在后台运行或尚未启动。 iOS 将调用 didFinishLaunchingWithOptions 或 didReceiveLocalNotification,或者根本不会触及您的应用。
请参阅本文了解详情 - http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html
关于objective-c - iOS 从非运行状态启动应用程序后处理通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12276427/