无法在Xcode的控制台中访问Push Notification PayLoad

时间:2019-01-31 15:45:50

标签: ios swift xcode firebase firebase-cloud-messaging

我正在尝试将通知推送到iOS设备,我正在使用FCM,我在设备上收到通知,但是我在Xcode的控制台中没有收到任何有效载荷,并且我想访问有效载荷,因为我计划将Notifications作为历史记录存储在UITableView中,因为它是一个Alerts应用程序。

我的有效载荷:

  

{           “ aps”:{               “警报”:{                   “ title”:“你好”,                   “身体”:“你好吗?”                   },                   “徽章”:0,                   “ sound”:“默认”,                   “ data”:“这是一个测试!”             }         }

//  AppDelegate.swift
//  P.T.S
//
//  Created by Hussein AlBehary on 1/19/19.
//  Copyright © 2019 Hussein AlBehary. All rights reserved.
//

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        debugPrint("didFinishLaunchingWithOptions")

        // Enable Push Notifications
        Messaging.messaging().delegate = self
        Messaging.messaging().shouldEstablishDirectChannel = true

        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()
        FirebaseApp.configure()

        return true
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    {
        Messaging.messaging().appDidReceiveMessage(userInfo)
        if let messageID = userInfo[gcmMessageIDKey]
        {
            print("Message ID: \(messageID)")
            debugPrint("Message ID: \(messageID)")
        }else if let myData = userInfo["data"] as? [String: AnyObject] {
            print(myData)
        }
        // Print full message.
        debugPrint(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
    {
        debugPrint("didRegisterForRemoteNotificationsWithDeviceToken: DATA")
        let token = String(format: "%@", deviceToken as CVarArg)
        debugPrint("*** deviceToken: \(token)")
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        debugPrint("deviceTokenString: \(deviceTokenString)")
        Messaging.messaging().apnsToken = deviceToken
        //        debugPrint("Firebase Token:",InstanceID.instanceID().token() as Any)
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Unable to register for remote notifications: \(error.localizedDescription)")
    }


}

// [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) {

        //Handle the notification ON APP
        Messaging.messaging().appDidReceiveMessage(notification.request.content.userInfo)
        completionHandler([.sound,.alert,.badge])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {

        //Handle the notification ON BACKGROUND
        Messaging.messaging().appDidReceiveMessage(response.notification.request.content.userInfo)
        completionHandler()
    }
}
// [END ios_10_message_handling]

extension AppDelegate : MessagingDelegate {
    // [START refresh_token]
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        InstanceID.instanceID().instanceID
            {
                (result, error) in
                if let error = error
                {
                    debugPrint("Error fetching remote instange ID: \(error)")
                }
                else if let result = result
                {
                    debugPrint("Remote instance ID token: \(result.token)")
                }
        }
        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }
    // [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)")
        debugPrint("Received data message: \(remoteMessage.appData)")
    }
    // [END ios_10_data_message]
}

尽管问题出在FCM仪表板上,所以我尝试了一个名为Pusher的MacOS应用并添加了有效载荷,但结果相同,我在设备上收到了通知,但在xcode中也没有。

0 个答案:

没有答案