我一直在尝试处理我的应用程序中的接收通知,但并没有真正奏效。
当我使用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函数,它工作得很好:我接收通知,[sharedLocalNotificationsInstanceprocessNotification: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
<Matrix-Morpheus-Meme title="WHAT IF I TOLD YOU">
当应用程序因用户点击本地通知警报而从"未运行"状态启动时,该应用程序是由iOS启动的,而不是由Xcode启动的,因此它不是在调试程序下运行的。您不能在其中断点,NSLog()也不会向Xcode控制台发送任何内容。使用UIAlertController
进行测试。
</Matrix-Morpheus-Meme>