iOS Firebase推送通知无法正常运行,但可以在推送器上运行

时间:2019-01-10 05:13:40

标签: swift firebase apple-push-notifications

问题:

鉴于我能够接收设备令牌和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推送通知的步骤:

  1. 下载了我的google-service.plist文件并将其拖到我的项目中
  2. 登录到我的Apple开发者帐户(收费)并创建带有推送通知的密钥(向下复制KeyID以及我的teamID或App前缀)。
  3. 进入我的Firebase帐户并上传我的密钥以及我的KeyID和TeamID
  4. 我确保我的应用程序功能将推送通知开关设置为开
  5. 尝试发送测试消息,但应用程序从未收到消息

我的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")

    }

}

其他信息:我可以通过使用pusher接收测试通知,只是想将其扔在那里,

谢谢

3 个答案:

答案 0 :(得分:0)

[按照此处定义的逐步过程进行操作。]

https://firebase.google.com/docs/cloud-messaging/ios/client

答案 1 :(得分:0)

请检查您的有效载荷。这可能是您未收到通知的原因。 您的有效载荷应该是:

{
    "to": "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification": {
        "body": "great match!",
        "title": "Portugal vs. Denmark",
        "icon": "myicon"
    },
    "data": {
        "Nick": "Mario",
        "Room": "PortugalVSDenmark"
    }
}

即使Firebase返回成功的请求,因为转发的APNS负载不正确,将所有数据保留在数据下也不会触发iOS中的任何通知。除了正确的方法之外,还应遵循GCM或FCM有效负载建议,即既将通知消息用于通知,又将数据用于自定义键/值对。

当FCM将数据发送到APNS时,它将其转换为APNs有效负载。它在aps标签中设置了通知值,即

{
    "aps" : {
        "alert" : {
            "title" : "Portugal vs. Denmark",
            "body" : "Portugal vs. Denmark",
        }
    },
    "Nick" : "Mario",
    "Room" : "PortugalVSDenmark"
}

答案 2 :(得分:0)

另一个可能的原因是禁用Firebase Swizzling,如此处所述: https://firebase.google.com/docs/cloud-messaging/ios/client