2017-01-01 84 views
-2

如何在swift中顯示UIAlertView中推送通知(FCM)的內容?swift:在UIAlertView中顯示推送通知的內容

link是沒有幫助的,因爲它是對的ObjectiveC ...

我要當接收到顯示UIAlertView中具有特定消息的客戶端推送通知(FCM)。例如:

如果( 「A」 從FCM接收的)顯示 「特定的消息1 UIAlertView中」,否則,如果( 「B」 從 FCM接收的)顯示 「特定的消息2中UIAlertView中」。

我只是嘗試這樣做,但它不工作對我來說:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 


     // display alert view when notification is received !!! 
     let alertController = UIAlertController(title: "Alert", message: "you have a new notification", preferredStyle: .alert) 
     let okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default) { 
      UIAlertAction in 
      NSLog("OK Pressed") 
     } 
     let cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.cancel) { 
      UIAlertAction in 
      NSLog("Cancel Pressed") 
     } 
     alertController.addAction(okAction) 
     alertController.addAction(cancelAction) 
     self.window?.rootViewController?.present(alertController, animated: true, completion: nil) 

} 
+0

...什麼都沒有... –

+0

這很難爲您調試。你甚至在iOS上收到通知嗎?如果手機被鎖定,您是否會在鎖屏上看到警報? – Alistra

+0

是的... iphone收到通知... –

回答

0

您是否獲得在控制檯中的任何錯誤,當您試圖提出警告?有時從AppDelegate呈現東西的時候,當我看到這樣的事情:

2017-01-01 16:04:14.806 wylt[46457:5781064] Warning: Attempt to present <UIAlertController: 0x7faf13f0a910> on <wylt.WYLTNavigationViewController: 0x7faf1402e200> whose view is not in the window hierarchy! 

我寧願找頂視圖控制器,並從那裏,而不是目前的警報......但是把呼叫呈現一個異步塊還曾內爲了我。

DispatchQueue.main.async { 
    self.window?.rootViewController?.present(alertController, animated: true, completion: nil) 
}