【发布时间】:2019-06-04 22:41:52
【问题描述】:
问题:
鉴于我能够接收设备令牌和 firebase 令牌,因此无法从 firebase 接收推送通知。
这是我在AppDelegate.swift 中的主要电话
UIApplication.shared.registerForRemoteNotifications()
此调用成功,我能够建立与 APNS 的连接,因为我能够通过此调用获取我的令牌:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
另外,我知道我正在连接到 firebase,因为我可以通过此调用获取我的 firebase 令牌:
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String)
所以我很有信心我在AppDelegate.swift 中的代码很好。
以下是我配置 Firebase 推送通知的步骤:
- 下载我的 google-service.plist 文件并将其拖到我的项目中
- 登录到我的苹果开发者帐户(付费)并创建带有推送通知的密钥(复制密钥 ID 以及我的团队 ID 或应用前缀)。
- 进入我的 firebase 帐户并上传我的密钥以及我的 KeyID 和 TeamID
- 我确保我的应用功能已将推送通知开关设为 ON
- 尝试发送测试消息,但应用从未收到任何消息
我的 AppDelegate.swift:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
UIApplication.shared.applicationIconBadgeNumber = 0
// Override point for customization after application launch.
loggingWithSwitftyBeaverConfiguration()
UNUserNotificationCenter.current().delegate = self
// requesting user's permission to accpet push notification
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted == true {
log.debug("User granted push notification")
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
log.debug("User did not grant pn")
}
}
return true
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
log.debug("Device token receiving failed because: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map {String(format: "%02.2hhx", $0)}.joined()
log.debug("Token: \(token)")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
log.debug("\(remoteMessage.description)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
log.debug("Firebase cloud messaging token: \(fcmToken)")
}
func applicationDidBecomeActive(_ application: UIApplication) {
Messaging.messaging().shouldEstablishDirectChannel = true
}
func applicationDidEnterBackground(_ application: UIApplication) {
Messaging.messaging().shouldEstablishDirectChannel = false
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
UIApplication.shared.applicationIconBadgeNumber += 1
print("push notification received")
}
}
额外信息:我可以使用推送器接收测试通知,只是想把它扔出去,
谢谢
更新:-工作代码:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var compositionRoot: CompositionRoot?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
configureCompositionRoot()
configureFirebase()
configurePushNotification()
configureApplicationEntryPoint()
configureLogging()
return true
}
func configureCompositionRoot() {
guard let window = window else { return }
compositionRoot = CompositionRoot(window: window)
}
func configureApplicationEntryPoint() {
guard let compositionRoot = compositionRoot else { return }
compositionRoot.getCoordinator().onStart()
}
func configureLogging() {
let console = ConsoleDestination()
log.addDestination(console)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map {String(format: "%02.2hhx", $0)}.joined()
print("Token: \(token)")
Messaging.messaging().apnsToken = deviceToken
//Messaging.messaging().setAPNSToken(deviceToken, type: MessagingAPNSTokenType.prod)
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func configureFirebase() {
FirebaseApp.configure()
}
func configurePushNotification() {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (granted, error) in
if granted == true {
print("User granted push notification")
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
print("User did not grant pn")
}
}
}
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
}
}
【问题讨论】:
-
您尝试从哪里发送推送通知?
-
关于 Firebase 云消息传递
-
你可以从这里appcoda.com/firebase-push-notifications检查所有步骤,也许你可以找到缺少的东西
-
我也试过这个方法,不适合我
-
尝试添加 --> func applicationDidBecomeActive(application: UIApplication) { FIRMessaging.messaging().connectWithCompletion { error in print(error) } }
标签: swift firebase apple-push-notifications