【问题标题】:how to show badge number and sound after receiving push notification via Firebase Cloud Messaging?通过 Firebase Cloud Messaging 收到推送通知后如何显示徽章编号和声音?
【发布时间】:2018-08-18 20:05:14
【问题描述】:

我正在尝试通过 Firebase Cloud Messaging 向我的 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

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

【问题讨论】:

    标签: ios swift firebase firebase-cloud-messaging


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 是的,你是对的,但我们该怎么做,如何在收到通知之前从我的应用程序中找到在我的设备上收到的通知数量,该应用程序位于 firebase 服务器中,并将其显示在徽章中?你能分享一些代码吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-19
        • 2022-11-30
        • 2020-12-26
        • 2014-08-09
        • 1970-01-01
        • 2016-11-03
        相关资源
        最近更新 更多