Ios 如何在通过Firebase云消息接收推送通知后显示徽章编号和声音?
我正在尝试通过Firebase云消息向我的iOS应用程序发送推送通知。我可以完美地设置firebase控制台和APN,我可以在设备中获得通过firebase控制台发送的通知 但是,当我收到通知时,它只显示警报,没有声音,徽章中没有数字,尽管我已经声明了Ios 如何在通过Firebase云消息接收推送通知后显示徽章编号和声音?,ios,swift,firebase,firebase-cloud-messaging,Ios,Swift,Firebase,Firebase Cloud Messaging,我正在尝试通过Firebase云消息向我的iOS应用程序发送推送通知。我可以完美地设置firebase控制台和APN,我可以在设备中获得通过firebase控制台发送的通知 但是,当我收到通知时,它只显示警报,没有声音,徽章中没有数字,尽管我已经声明了UNAuthorizationOptions=[.alert、.badge、.sound]这是我在应用程序代理中使用的代码 import UIKit import Firebase import UserNotifications @UIAppl
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”
设置为是。这是我的播客文件
# 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
那么,当我收到通知时,如何添加声音和徽章?您需要告诉您的服务/后端所有者发送与此通知类似的有效负载。基本上,您需要有
徽章
和声音
键,才能按预期工作:
{
"aps":{
"alert":"This is a message",
"badge":1,
"sound":"default"
}
}
我认为当您收到通知时,最好在应用程序中正确处理徽章,这样您就可以在收到通知之前根据数据和当前徽章计数将其设置为任意数字。默认情况下,如果您从Firebase控制台发送消息,则默认情况下,徽章和声音被禁用,请在Firebase控制台中打开“撰写消息”底部的“高级选项”,并“启用”徽章和下面的声音图片
是的,你是对的,但我们如何才能做到这一点,在收到通知并将其显示在徽章之前,如何从firebase服务器中的我的应用程序中查找我的设备上接收到的通知数量?你能分享一些代码吗?