我一直試圖在我的應用程序中處理接收通知,但它並沒有真正解決問題。iOS從未運行狀態啓動應用程序後處理通知
當我使用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功能,它完美的作品:我收到通知,並[sharedLocalNotificationsInstance processNotification:通知]完美的作品。
- (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];
}
嗨拉爾斯克。我有同樣的確切問題。 我可以在應用程序處於前景或背景時捕獲通知,但有趣的事情發生在應用程序關閉時。 我點擊通知,應用程序打開,在屏幕上停留1秒,然後屏幕變黑。我在後臺看到應用程序,但沒有收到通知的跡象。我真的不確定是否 UILocalNotification * notification =(UILocalNotification *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 是否有效。 您是否找到了解決方案? – Add080bbA