ios - 当用户按下图标而不是警报时处理本地通知

标签 ios objective-c notifications uilocalnotification localnotification

这是我想处理的情况,引用自 Apple 的文档。

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 application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.

没有通知信息怎么办?

最佳答案

如果我对您的理解正确,听起来您有一个 UILocalNotification 已被触发,但如果用户点击应用程序图标而不是通知,您仍然需要处理它。正确吗?

如果是这种情况,那么据我所知,您将无法处理来自应用程序委托(delegate)的通知,因为应用程序不是通过通知启动或退出后台的,而是由用户的互动。

但是,如果您在带有通知的应用程序上设置 badgeNumber,那么您可以尝试这样的操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
} else if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
    // Assume that user launched the app from the icon with a notification present.
}}

您可能还必须检查 - (void)applicationDidBecomeActive:(UIApplication *)application 中的 badgeNumber。

关于ios - 当用户按下图标而不是警报时处理本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24540304/

相关文章:

ios - 不再使用时从 UITableViewCell 中移除观察者

notifications - 禁用 gulp 通知?

iphone - 如何在导航栏中添加图像

ios - 使用数据API的iPhone中的YouTube热门视频列表

ios - 快速选择特定文本字段时移动 View

html - 禁用 iPad 上的 HTML5 <video> 内联播放

ios - 我如何知道 NSCountedSet 中 Array 对象的索引

objective-c - 类别实例方法不调用

android - 在整个应用程序中全局广播和接收连接更改的最佳做法是什么?

javascript - 使用通知 API 时如何向通知添加 URL?