Handling local notifications when the user presses the icon instead of the alert

依然范特西╮ 提交于 2019-12-05 20:53:47

If I understand you correctly, it sounds like you have a UILocalNotification that has been fired, but you need to still handle it if the user taps the application icon instead of the notification. Correct?

If this is the case, then to my knowledge you won't be able to handle the notification from the app delegate, because the app is not being launched or brought out of the background by the notification, but instead by the user's interaction.

However, if you are setting a badgeNumber on the application with the notification then you could try something like this:

- (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.
}}

You may also have to check the badgeNumber in - (void)applicationDidBecomeActive:(UIApplication *)application as well.

Improve to @Aron Crittendon answer:

Consider also to handle that in applicationDidBecomeActive:

-(void)applicationDidBecomeActive:(UIApplication *)application
{
    if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
        //application is in background, fired notification and user tapped app icon with badge
    }
}

As the documentation states, if you tap the icon on iOS (and not the notification's alert/banner) then the same method is called but you get no notification information. There is no way to handle a local notification simply by tapping the app icon.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!