2017-08-28 1 views
0

Ich implementiere Push-Benachrichtigungen in iOS mit Firebase. Leider habe ich ein Problem, bei dem Firebase die Benachrichtigung nicht erhält.Firebase-Benachrichtigung nicht erhalten auf iOS 10

Dies ist der Fehler, den ich bin auf der Ausgabe bekommen:

****[Firebase/Messaging][I-FCM002019] FIRMessaging received data message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented**** 

Wie löse ich dieses Problem? Ich habe es auf iOS 9.3 getestet und es funktioniert gut. Ich verwende den folgenden Code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
    { 
     FirebaseApp.configure() 
     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) 
     } 
     application.registerForRemoteNotifications() 
     // Add observer for InstanceID token refresh callback. 
     NotificationCenter.default 
      .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotification), 
         name: NSNotification.Name.InstanceIDTokenRefresh, object: nil) 
    return true 
} 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) 
    { 
     print(userInfo) 
} 
    func tokenRefreshNotification(_ notification: UIViewController) 
    { 
     if let refreshedToken = InstanceID.instanceID().token() 
     { 
      print("InstanceID token: \(refreshedToken)") 
      UserDefaults.standard.set(refreshedToken, forKey: "Device_token") 
      UserDefaults.standard.synchronize() 
     } 
     // Connect to FCM since connection may have failed when attempted before having a token. 
     connectToFcm() 
    } 

    // [START connect_to_fcm] 
    func connectToFcm() 
    { 
     Messaging.messaging().shouldEstablishDirectChannel = true 
    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
    { 

     //Tricky line 
     Messaging.messaging().apnsToken = deviceToken 
     //InstanceID.instanceID().setAPNSToken(deviceToken, type: InstanceIDAPNSTokenType.unknown) 
     print("Device Token:", tokenString) 
    } 

    func applicationDidBecomeActive(_ application: UIApplication) { 
     print("applicationDidBecomeActive ") 

     connectToFcm() 
    } 

Antwort

1

Sie nicht Delegatmethode für IOS 10, und auch überprüfen Sie Ihre Nutzlast hinzugefügt haben, haben mutable-content = true:

import UIKit 
import UserNotifications 

import Firebase 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 
let gcmMessageIDKey = "gcm.message_id" 

func application(_ application: UIApplication, 
       didFinishLaunchingWithOptions launchOptions: 
[UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

FirebaseApp.configure() 

// [START set_messaging_delegate] 
Messaging.messaging().delegate = self 
// [END set_messaging_delegate] 
// Register for remote notifications. This shows a permission dialog on 
first run, to 
// show the dialog at a more appropriate time move this registration 
accordingly. 
    // [START register_for_notifications] 
    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) 
} 

application.registerForRemoteNotifications() 

// [END register_for_notifications] 
return true 
} 

// [START receive_message] 
func application(_ application: UIApplication, 
didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
// 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 
// With swizzling disabled you must let Messaging know about the 
message, for Analytics 
// Messaging.messaging().appDidReceiveMessage(userInfo) 
// Print message ID. 
    if let messageID = userInfo[gcmMessageIDKey] { 
    print("Message ID: \(messageID)") 
    } 

    // Print full message. 
    print(userInfo) 
    } 

    func application(_ application: UIApplication, 
    didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping 
    (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 
// With swizzling disabled you must let Messaging know about the 
message, for Analytics 
// Messaging.messaging().appDidReceiveMessage(userInfo) 
// Print message ID. 
if let messageID = userInfo[gcmMessageIDKey] { 
    print("Message ID: \(messageID)") 
} 

// Print full message. 
print(userInfo) 

completionHandler(UIBackgroundFetchResult.newData) 
} 
// [END receive_message] 
func application(_ application: UIApplication, 
didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    print("Unable to register for remote notifications: \ 
(error.localizedDescription)") 
    } 

// This function is added here only for debugging purposes, and can be 
    removed if swizzling is enabled. 
// If swizzling is disabled then this function must be implemented so 
that the APNs token can be paired to 
// the FCM registration token. 
func application(_ application: UIApplication, 
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
print("APNs token retrieved: \(deviceToken)") 

// With swizzling disabled you must set the APNs token here. 
// Messaging.messaging().apnsToken = deviceToken 
} 
} 

// [START ios_10_message_handling] 
@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 

    // With swizzling disabled you must let Messaging know about the 
    message, for Analytics 
// Messaging.messaging().appDidReceiveMessage(userInfo) 
// Print message ID. 
if let messageID = userInfo[gcmMessageIDKey] { 
    print("Message ID: \(messageID)") 
} 

// Print full message. 
print(userInfo) 

// Change this to your preferred presentation option 
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() 
    } 
} 
// [END ios_10_message_handling] 

extension AppDelegate : MessagingDelegate { 
// [START refresh_token] 
func messaging(_ messaging: Messaging, didRefreshRegistrationToken 
fcmToken: String) { 
    print("Firebase registration token: \(fcmToken)") 
    } 
// [END refresh_token] 
// [START ios_10_data_message] 
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) 
when the app is in the foreground. 
// To enable direct data messages, you can set 
    Messaging.messaging().shouldEstablishDirectChannel to true. 
    func messaging(_ messaging: Messaging, didReceive remoteMessage: 
    MessagingRemoteMessage) { 
    print("Received data message: \(remoteMessage.appData)") 
    } 
    // [END ios_10_data_message] 
} 

oder folgen Sie diesem Link: -

https://github.com/firebase/quickstart- 
ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift 
+0

funktioniert nicht. Ich implementiere auch diese Delegiertenfunktion, zeige aber die Benachrichtigung nicht an. –

+0

folgen Sie diesem Link: - https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift – Pushpendra

Verwandte Themen