【问题标题】:iOS Push Notifications doesn't work with FirebaseiOS 推送通知不适用于 Firebase
【发布时间】:2018-05-25 21:48:25
【问题描述】:

我使用 Firebase 通过我的 iOS 应用发送通知,我已按照文档中的所有步骤操作:

  • Apple 开发者帐户配置

  • 生成 CSR 文件

  • 正在上传 CSF 文件

  • 准备 APNs 证书

  • 为推送通知配置 Firebase

  • 在我的应用中构建 Firebase 通知

当我尝试发送通知时,对于某个特定用户或所有 iOS 设备,在 Firebase 中都没有发现问题。但是我的设备(显然是真实设备)都没有收到通知。

我使用了好的 Bundle,我在我的应用程序中启用了通知,并且我的 AppDelegate 中有代码:

import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    UINavigationBar.appearance().barTintColor = Design.blue_es
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    UINavigationBar.appearance().tintColor = UIColor.white


    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().remoteMessageDelegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FirebaseApp.configure()

    return true
}
func application(received remoteMessage: MessagingRemoteMessage) {
    print(remoteMessage.appData)
}

// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // Print the error to console (you should alert the user that registration failed)
    print("APNs registration failed: \(error)")
}

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
}
}

【问题讨论】:

  • 您是否在 Firebase 控制台上上传了无密钥的推送通知证书的 P12 证书?
  • 我上传了P12证书,但是我不明白你说的“没有密钥”是什么意思
  • 当您从 Keychain 导出证书时,您会看到 Apple Push 名称左侧的一个下拉箭头 展开它您将看到 Key 图标
  • 好的,我已经上传了带有钥匙串的 P12 证书用于生产和开发通知,但我使用相同的 CSR 来生成它们

标签: ios firebase swift3 push-notification


【解决方案1】:

IN Swift 3

  1. 配置您的 Apple 开发者帐户

  2. 生成 CSR 文件

  3. 上传您的 CSR 文件

  4. 准备 APNs 证书

  5. 为推送通知配置 Firebase

  6. 构建 Firebase 通知应用

  7. 使用 CocoaPods 安装 Firebase SDK

  8. 添加 GoogleService-Info.plist

  9. 启用推送通知

  10. 初始化推送通知

  11. 您项目的 AppDelegate

    import UIKit
    import SVProgressHUD
    import UserNotifications
    import Firebase
    import FirebaseInstanceID
    import FirebaseMessaging
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate, MessagingDelegate {
    
        var window: UIWindow?
    
    
        static var appDelegate:AppDelegate!
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
    
            if #available(iOS 10.0, *) {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.current().delegate = self
                let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
                UNUserNotificationCenter.current().requestAuthorization(
                    options: authOptions,
                    completionHandler: {_, _ in })
                // For iOS 10 data message (sent via FCM
                Messaging.messaging().remoteMessageDelegate = self
            } else {
                let settings: UIUserNotificationSettings =
                    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }
    
            application.registerForRemoteNotifications()
    
            FirebaseApp.configure()
    
            return true
    
        }
    
        func application(received remoteMessage: MessagingRemoteMessage) {
            print(remoteMessage.appData)
        }
    
        func application(application: UIApplication,
                         didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
            Messaging.messaging().apnsToken = deviceToken as Data
        }
    

    }

【讨论】:

  • 我做了一切,我真的不知道我的错误是什么。开发者和生产 APNs 证书有区别吗?
  • 我没有任何错误,firebase 正确发送通知,但我的设备都没有收到通知。
  • 您将在您的应用程序中进入 firebase,然后进入项目设置和项目设置 -> 云消息传递,然后选择 iOS 应用程序并添加 APN 的证书
【解决方案2】:

我想首先为 firebase 配置

FirebaseApp.configure()

之后你应该调用 registerForRemoteNotifications()

【讨论】:

  • 我尝试了,但没有任何改变
  • 您是否启用了来自功能的推送通知?目标 > 功能 > 推送通知
  • 是的,有没有办法在没有 firebase 的情况下测试通知,看看问题是否来自 Firebase?
猜你喜欢
  • 2020-05-22
  • 2021-03-31
  • 2017-01-22
  • 2017-04-09
  • 2016-11-29
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
相关资源
最近更新 更多