iOS从非运行状态启动应用程序后处理通知

laa*_*rsk 8 objective-c uiapplication ios uilocalnotification

我一直在尝试处理在我的应用程序中接收通知,但它并没有真正解决.

我用的时候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;
}
Run Code Online (Sandbox Code Playgroud)

第二段代码: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];
}
Run Code Online (Sandbox Code Playgroud)

小智 4

这就是 iOS 处理本地通知的方式。这取决于您的应用程序的状态,例如活动、在后台运行或尚未启动。iOS 将调用 didFinishLaunchingWithOptions 或 didReceiveLocalNotification,或者根本不会触及您的应用程序。

请参阅这篇文章进行澄清 - http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html