0

我已经按照他们网站上的说明实施了 Firebase 云消息传递。所以当我从这里生成通知时: 在此处输入图像描述

AppDelegate.swift我添加了didReceiveRemoteNotification以下功能:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],

     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        Messaging.messaging().appDidReceiveMessage(userInfo)
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

我实施的其他方法是
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String)
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

而我的didFinishLaunchingWithOptions有:

 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 })
            // For iOS 10 data message (sent via FCM
            Messaging.messaging().delegate = self
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

        FirebaseApp.configure()

        NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)

        return true
4

1 回答 1

1

请看一下 FCM 的这个 gist 文件 AppDelegate 文件

在 XCode Goto Targets > Capabilities & 确保您的推送通知已打开。 在此处输入图像描述 希望能帮助到你。

于 2017-09-21T11:07:29.470 回答