Ale*_*289 3 ios firebase swift firebase-cloud-messaging
我正在尝试通过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)")
}
}
Run Code Online (Sandbox Code Playgroud)
我"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
Run Code Online (Sandbox Code Playgroud)
那么在收到通知时如何添加声音和徽章?
您需要告诉您的服务/后端所有者发送类似于此通知的有效负载.基本上你需要让它sound和badge键按照你的期望工作:
{
"aps":{
"alert":"This is a message",
"badge":1,
"sound":"default"
}
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,如果您从 Firebase 控制台发送消息,默认情况下徽章和声音是禁用的,请打开 Firebase 控制台“撰写消息”底部的“高级选项”,并“启用”徽章和声音下图。
| 归档时间: |
|
| 查看次数: |
2390 次 |
| 最近记录: |