iOS UILocalNotification - 当应用程序在后台运行时触发没有委托方法,并且在通知时单击图标 [英] iOS UILocalNotification - No delegate methods triggered when app is running in background and the icon is clicked upon notification
问题描述
iPhone版本 - 5.1(9B176)
iPhone version - 5.1 (9B176)
以下是本地通知中的一系列事件,其中 didFinishLaunchingWithOptions
方法未被调用。
Below is the series of events during Local Notification where in which didFinishLaunchingWithOptions
method is not invoked.
- 应用程序在后台运行。
- 收到本地通知 - 简单通知无对话。
- 点击显示徽章编号的应用程序图标。
预计按 Apple文档:
作为提交通知的结果,用户点击警报的操作按钮或点击(或单击)应用程序图标。
如果点击操作按钮(在运行iOS的设备上),系统启动应用程序,应用程序调用其委托的didFinishLaunchingWithOptions
方法(如果已实现);它传入通知有效负载(用于远程通知)或本地通知对象(用于本地通知)。
As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s
didFinishLaunchingWithOptions
method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
如果在设备上轻击应用程序图标运行iOS时,应用程序调用相同的方法,但不提供有关通知的信息
实际:
didFinishLaunchingWithOptions
未调用。但是调用了 applicationWillEnterForeground
和 applicationDidBecomeActive
。
Actual :
didFinishLaunchingWithOptions
NOT invoked. But applicationWillEnterForeground
and applicationDidBecomeActive
were invoked.
推荐答案
你是对的。该行为与文档不一致。将文档放在一边,重点关注实际行为;问题的症结似乎是这样:如果用户通过与通知交互而变为活动状态,您将收到指向通知的指针,如果用户直接点击您的应用程序图标,则不会。
You are correct. The behavior is inconsistent with the documentation. Putting the documentation aside and focusing on actual behavior; The crux of the matter seems to be this: If your app becomes active by the user interacting with the notification you will receive a pointer to the notification, if the user taps your application icon directly you will not.
为了说明。如果您提出警报样式通知,并且用户点击操作按钮,或者如果您在案例中显示横幅通知并且用户点击该通知,您将通过以下两种方式之一接收指向通知的指针:
To illustrate. If you present an alert style notification and the user taps the action button, or if, as in your case, you present a banner notification and the user taps on that you will receive a pointer to the notification in one of two ways:
如果您的应用程序处于未运行状态:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UILocalNotification *launchNote = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (launchNote){
// I recieved a notification while not running
}
}
如果您的应用程序在任何州运行:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
}
如果用户选择取消提醒样式通知,该通知就会消失。
In the case where a user elects to cancel an alert style notification, that notification is gone.
真正恼人的不一致的部分是,如果您提出横幅通知并且用户点击您的图标,您似乎无法在通知中心检索对所呈现的通知的引用。即它们不会出现在 [[UIApplication sharedApplication] scheduledLocalNotifications]
数组中,大概是因为它们不再被安排但现在已经出现。
The truly annoying an inconsistent part is that if you present a banner notification and the user taps your icon you seem to have no way of retrieving a reference to the presented notifications in the notification center. i.e. they do not appear in the [[UIApplication sharedApplication] scheduledLocalNotifications]
array, presumably because they are no longer scheduled but are now presented.
简而言之;文档错了。还有其他烦人的不一致。如果这种行为对您来说是一个问题,那么您应该向Apple提交一个错误。
So in short; The documentation is wrong. And there are other annoying inconsistencies. If this behavior is a problem for you you should file a bug with Apple.
这篇关于iOS UILocalNotification - 当应用程序在后台运行时触发没有委托方法,并且在通知时单击图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!