我正在尝试通过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)")
}
}
我还在"FirebaseAppDelegateProxyEnabled"
Info.plist中设置为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
那么当我收到通知时如何添加声音和徽章?
您需要告诉您的服务/后端所有者,为与此通知类似的通知发送有效负载。基本上,您需要具有badge
和sound
键才能使其按预期工作:
{
"aps":{
"alert":"This is a message",
"badge":1,
"sound":"default"
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句