2017-01-01 76 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) 
}