如何通过Firebase云消息接收推送通知后显示徽章编号和声音?

时间:2018-03-10 09:32:09

标签: ios swift firebase firebase-cloud-messaging

我正在尝试通过Firebase云消息传递将推送通知发送到我的iOS应用。我可以完美地设置firebase控制台和APN,我可以在我的设备中获取通过Firebase控制台发送的通知。

但是,当我收到通知时,它只显示警报,没有声音,徽章中没有数字,即使我已经声明UNAuthorizationOptions = [.alert, .badge, .sound]这里是我在应用代表中使用的代码

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

    }






}

我还在Info.plist中将"FirebaseAppDelegateProxyEnabled"设置为YES。这是我的podfile

# Uncomment the next line to define a global platform for your project
 platform :ios, '9.0'

target 'Firebase Push Notification' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Firebase Push Notification

    pod 'Firebase/Core'
    pod 'Firebase/Messaging'

end

那么在收到通知时如何添加声音和徽章?

3 个答案:

答案 0 :(得分:5)

您需要告知您的服务/后端所有者发送类似于此通知的有效负载。基本上,您需要使用soundbadge键才能按预期工作:

{  
   "aps":{  
      "alert":"This is a message",
      "badge":1,
      "sound":"default"
   }
}

答案 1 :(得分:4)

默认情况下,如果您从Firebase控制台发送消息,默认情况下会禁用徽章和声音,请在Firebase控制台的“撰写邮件”底部打开“高级选项”,然后“启用”徽章和声音如下图所示。

enter image description here

答案 2 :(得分:0)

我认为,当您收到通知时,最好在您的应用中处理徽章,这样您就可以在收到通知之前根据数据和当前徽章数将其设置为任意数字