【发布时间】:2018-08-18 20:05:14
【问题描述】:
我正在尝试通过 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)")
}
}
我还在 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
那么当我收到通知时如何添加声音和徽章?
【问题讨论】:
标签: ios swift firebase firebase-cloud-messaging