代码之家  ›  专栏  ›  技术社区  ›  Alexa289

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

  •  4
  • Alexa289  · 技术社区  · 8 年前

    我正在尝试通过Firebase云消息向iOS应用程序发送推送通知。我可以完美地设置firebase控制台和APN,我可以在设备中通过firebase控制台发送通知。

    但是,当我收到通知时,它只显示警报,没有声音,徽章上没有数字,尽管我已经声明 UNAuthorizationOptions = [.alert, .badge, .sound] 以下是我在app delegate中使用的代码

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

    我还设置 "FirebaseAppDelegateProxyEnabled" 在我的信息中选择“是”。普利斯特。这是我的播客文件

    # 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 回复  |  直到 8 年前
        1
  •  6
  •   badhanganesh Milan Nosáľ    5 年前

    您需要告诉您的服务/后端所有者为类似于此的通知发送负载。基本上你需要 badge sound 使其按预期工作的关键:

    {  
       "aps":{  
          "alert":"This is a message",
          "badge":1,
          "sound":"default"
       }
    }
    
        2
  •  5
  •   Alexa289    8 年前

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

    enter image description here

        3
  •  0
  •   Otto Gutierrez    8 年前

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