收到推送通知后如何处理徽章和通知声音?

时间:2018-03-11 22:20:17

标签: ios swift firebase firebase-cloud-messaging

我在我的应用中实施 Firebase 推送通知。我可以收到我从 Firebase 控制台发送的提醒消息,但它只是警报,没有声音,没有徽章。

所以我认为我必须自己使用import UserNotifications处理徽章和声音,所以我该怎么做?

以下是我用来实现推送通知的代码:

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {

    var window: UIWindow?


    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 as? UNUserNotificationCenterDelegate

            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()

        Messaging.messaging().delegate = self
        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "")")
        return true
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
        print("Firebase registration token: \(fcmToken)")

    }
}

0 个答案:

没有答案