2016-07-07 35 views
0

當應用程序關閉時,我有FCM通知工作,但當應用程序打開並在前臺運行時,我無法讓它們正常工作。 「Swift和Firebase雲消息傳遞通知僅在應用程序泄露時起作用

我收到以下錯誤」警告:應用程序委託收到對應用程序的調用:didReceiveRemoteNotification:fetchCompletionHandler:但完成處理程序從未調用。我一直在谷歌搜索幾個小時,閱讀firebase文檔,但我不確定如何克服這個錯誤。

// [START receive_message] 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
       fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    // If you are receiving a notification message while your app is in the background, 
    // this callback will not be fired till the user taps on the notification launching the application. 
    // TODO: Handle data of notification 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print("%@", userInfo) 

    ////new code to help with error but no luck 
    if application.applicationState == UIApplicationState.Active { 
     print("%@", userInfo) 
     print("message recieved") 
    } 
} 

加入以下代碼沒有工作

if application.applicationState == UIApplicationState.Active { 
     print("%@", userInfo) 
     print("message recieved") } 

任何幫助或建議,將不勝感激

+0

我沒有做任何更改,但現在我可以在應用程序關閉時收到通知,但是當它打開時它不會崩潰但不顯示通知。我可以在控制檯中打印通知。我想我需要創建一個本地通知或東西 –

回答

0

您收到此錯誤 Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.因爲你沒有返回FechedCompletionHandler

你應該回到這個completionHandler用在適當的地方適當UIBackgroundFetchResult值,UIBackgroundFetchResult是一個枚舉誰的價值觀是:

  • NewData
  • 的無數據
  • 失敗

例如:閣下didReceiveRemoteNotification將變爲:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
      fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
// If you are receiving a notification message while your app is in the background, 
// this callback will not be fired till the user taps on the notification launching the application. 
// TODO: Handle data of notification 

// Print message ID. 
print("Message ID: \(userInfo["gcm.message_id"]!)") 

// Print full message. 
print("%@", userInfo) 

////new code to help with error but no luck 
if application.applicationState == UIApplicationState.Active { 
    print("%@", userInfo) 
    print("message recieved") 
    completionHandler(.NewData) 
} 

}

+0

謝謝你的答覆!很高興現在:) –

+0

很高興聽到這個,快樂編碼:) – NSIceCode

相關問題