正确的方式来检索FCM的令牌 - iOS 10 Swift 3 [英] Correct way to retrieve token for FCM - iOS 10 Swift 3

查看:207
本文介绍了正确的方式来检索FCM的令牌 - iOS 10 Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过FirebaseAuth / FCM等实现了Firebase,并通过Firebase控制台成功发送了通知。



然而,我需要从我自己的应用程序服务器推送通知。



我想知道下面哪个方式是正确的方法来检索设备的注册ID:
$ b $ 1)从didRegisterForRemoteNotificationWithDeviceToken检索注册ID令牌

  func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data){
var token =

for i在0 ..< deviceToken.count {
token + = String(format:%02.2hhx,arguments:[deviceToken [i]])
}

print (Registration succeeded!)
print(Token:,token)
Callquery(令牌)

$ b $ / code> pre>
$ b 2)从firebase检索注册令牌(基于检索当前注册令牌的Firebase文档)

  let token = FIRInstanceID.instanceID()。token()! 

我正在使用第一种方式,即使注册ID存储在我的应用程序服务器数据库相应地,我得到这个CURL会话结果: - $ /

$ p $ {code $ {multicast_id:6074293608087656831,success:0 ,failure:1,canonical_ids:0,results:[{error:InvalidRegistration}]}

我也尝试了第二种方法,并在运行应用程序时出现致命错误,如下所示: -



赞赏如果有人能指点我的话,谢谢!

解决方案 tokenRefreshNotification 函数在启动应用程序时并不总是被调用。

然而,当把代码放在常规的 didRegisterForRemoteNotificationsWithDeviceToken 委托函数,每次都可以得到令牌:

pre $ func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data) {
if refreshedToken = FIRInstanceID.instanceID()。token(){
print(InstanceID token:\(refreshedToken))
}
}

(swift 3)

i had implement Firebase with FirebaseAuth/FCM etc and did sent notification successfully through Firebase Console.

However i would need to push the notification from my own app server.

i am wondering below which way is correct way to retrieve the registration id for the device:-

1) retrieve registration id token from didRegisterForRemoteNotificationWithDeviceToken

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var token = ""

    for i in 0..<deviceToken.count {
        token += String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }

    print("Registration succeeded!")
    print("Token: ", token)
    Callquery(token)

}

2) Retrieve Registration token from firebase (Based on Firebase document which retrieve the current registration token)

let token = FIRInstanceID.instanceID().token()!

i was using the first way, the push notification is not being received even the registration id is stored on my app server database accordingly and i get this CURL session result :-

{"multicast_id":6074293608087656831,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

i had also tried the second way and get fatal error while running the app as below:-

appreciated if anyone could point me the right way, thanks!

解决方案

The tokenRefreshNotification function doesn't always get called when launching the app.

However, when placing the code inside the regular didRegisterForRemoteNotificationsWithDeviceToken delegate function, I can get the token every time:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
    }
}

(swift 3)

这篇关于正确的方式来检索FCM的令牌 - iOS 10 Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆