gpt4 book ai didi

ios - 如何在通过 Firebase Cloud Messaging 收到推送通知后显示角标(Badge)编号和声音?

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:09 32 4
gpt4 key购买 nike

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

但是,当我收到通知时,它只显示警报,没有声音,角标(Badge)中没有数字,即使我已经说明了 UNAuthorizationOptions = [.alert, .badge, .sound]这是我在应用程序委托(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)")

}






}

我还在我的 Info.plist 中将 "FirebaseAppDelegateProxyEnabled" 设置为 YES。这是我的播客文件

# 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

那么如何在收到通知时添加声音和角标(Badge)呢?

最佳答案

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

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

关于ios - 如何在通过 Firebase Cloud Messaging 收到推送通知后显示角标(Badge)编号和声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49207511/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com