我正在尝试通过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
那么在收到通知时如何添加声音和徽章?
答案 0 :(得分:5)
您需要告知您的服务/后端所有者发送类似于此通知的有效负载。基本上,您需要使用sound
和badge
键才能按预期工作:
{
"aps":{
"alert":"This is a message",
"badge":1,
"sound":"default"
}
}
答案 1 :(得分:4)
答案 2 :(得分:0)
我认为,当您收到通知时,最好在您的应用中处理徽章,这样您就可以在收到通知之前根据数据和当前徽章数将其设置为任意数字