2017-07-17 23 views
0

我試圖實現我的應用程序火力點雲的消息。didReceiveRemoteNotification前景火力雲消息不叫於iOS

我使用下面的代碼

if #available(iOS 10.0, *) { 
      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.current().delegate = self 

      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
      UNUserNotificationCenter.current().requestAuthorization(
       options: authOptions, 
       completionHandler: {_, _ in }) 
     } else { 
      let settings: UIUserNotificationSettings = 
       UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
     } 

,一切工作正常時,應用程序是在回地面註冊的通知,但是當應用程序在前臺,didReceiveRemoteNotification根本沒有被調用。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    // Print message ID. 
    print("please help") 
    if let messageID = userInfo[gcmMessageIDKey] { 
     print("Message ID: \(messageID)") 
    } 

    // Print full message. 
    print(userInfo) 
    completionHandler(.newData) 
} 

任何人都知道這個可能的錯誤?謝謝!

編輯:我不需要看到的通知顯示,當應用程序在前臺,我只是想從通知

+0

這個完成處理程序根本不能調用嗎? –

+0

是否聲明UNUserNotificationCenterDelegate? – Mohit

回答

1

的iOS 10.x的處理消息有處理推送通知時,在一個新的功能前景,你應該實現這一個:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

    completionHandler(
     [UNNotificationPresentationOptions.alert, 
     UNNotificationPresentationOptions.sound, 
     UNNotificationPresentationOptions.badge]) 

} 
0

請執行以下方法來接收前景推送通知

對於iOS10,Swift3:

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

對於iOS10,雨燕2.3:

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) 
{ 
    //Handle the notification 
    completionHandler(
     [UNNotificationPresentationOptions.Alert, 
     UNNotificationPresentationOptions.Sound, 
     UNNotificationPresentationOptions.Badge]) 
} 

嘗試這一次,

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

    print("Recived: \(userInfo)") 

    completionHandler(.NewData) 

} 
+0

我在哪裏實施此方法?順便說一句,iOS 9及以下版本呢? – progammingBeignner

+0

在appdelegate中執行此操作。對於iOS9及以下版本,您不需要iOS9中的此方法 –

+0

檢查是否正常工作。它應該工作我認爲。 –

0

如果使用推送通知火力地堡的IOS 10 請添加此代碼。希望能幫助你解決這個問題:

@available(iOS 10, *) 
extension AppDelegate : UNUserNotificationCenterDelegate { 

    // Receive displayed notifications for iOS 10 devices. 
    func userNotificationCenter(_ center: UNUserNotificationCenter, 
           willPresent notification: UNNotification, 
           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     let userInfo = notification.request.content.userInfo 
     // Print message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print(userInfo) 
     completionHandler([]) 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, 
           didReceive response: UNNotificationResponse, 
           withCompletionHandler completionHandler: @escaping() -> Void) { 
     let userInfo = response.notification.request.content.userInfo 
     // Print message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print(userInfo) 
     completionHandler() 
    } 
} 

extension AppDelegate : FIRMessagingDelegate { 
    // Receive data message on iOS 10 devices while app is in the foreground. 
    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
     print(remoteMessage.appData) 
    } 
} 
相關問題