2017-08-31 45 views
0

我已檢查所有通知都已正確安排在通知中心上。而且,我的應用委託中包含completionHandler([.alert, .badge, .sound])代碼,以確保即使用戶使用該應用時通知也會顯示出來。當應用程序運行時本地通知不會彈出,但會在應用程序處於後臺時彈出

結果是通知將只顯示應用程序在後臺,而不是其他方式。

+0

你處理收到通知在AppDelegate中(當應用程序被激活)? –

+0

你可以檢查這個鏈接 - https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3 我希望這會爲你工作。 –

+0

請檢查此鏈接 - https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3 –

回答

0

你是否像這樣處理應用程序委託中的本地通知?

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
    // Do something you want 
    println("Received Local Notification:") 
    println(notification.alertBody) 
} 

以上是本地通知並沒有對hadle遠程通知的另一個方法,

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
// Do something you want 
} 

確保您正在處理使用本地通知方法的本地通知。

+0

是的!我做到了。我使用:func userNotificationCenter(_ center:UNUserNotificationCenter,didReceive響應:UNNotificationResponse,withCompletionHandler completionHandler:@escaping() - > Void){ –

0

請試試這個:

extension AppDelegate: UNUserNotificationCenterDelegate { 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     completionHandler([.alert, .sound]) 
    } 
} 

你可以找到Apple doc here

我知道的是,無論我發送給completionHandler的選項都會發生。

如果您只發送.alert,它只會顯示警報 如果您只發送.sound,它將播放通知中指定的聲音。

如果我們發送兩個選項,將會同時執行這兩個操作。

如果我們不發送任何參數,將不會做任何事情。

所以你需要做的是發送參數爲.alert

didFinishLaunchingWithOptions您還需要添加

UNUserNotificationCenter.current().delegate = self 
相關問題