提供的注册令牌无效。确保它与客户端应用从 FCM 注册时收到的注册令牌匹配

Posted

技术标签:

【中文标题】提供的注册令牌无效。确保它与客户端应用从 FCM 注册时收到的注册令牌匹配【英文标题】:Invalid registration token provided. Make sure it matches the registration token the client app receives from registering with FCM 【发布时间】:2019-04-30 23:06:17 【问题描述】:

我从 XCode 控制台上的客户端 ios 应用程序中获得了此代码

Firebase 注册令牌:diWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7h diWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7h

NodeJS

console.log("START");
var FCM = require('fcm-node');
var serverKey = require('/Users/bheng/Desktop/Apps/APNS/node/mhn-app-firebase-adminsdk-bs45c-5ac3770488.json')
var fcm = new FCM(serverKey)
var collapseKey = 'new_message';
var message = 
    to: 'diWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7hdiWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7h',
    data: 
        cpeMac: '000000000000',
        type: 'malware'
    ,
    notification: 
        title: 'Hello baby',
        body: 'Nice body',
        tag: collapseKey,
        icon: 'ic_notification',
        color: '#18d821',
        sound: 'default',
    ,
;


fcm.send(message, function(err, response)
    if (err) 
        console.log("Something has gone wrong!")

        console.log(JSON.stringify(err));

     else 
        console.log("Successfully sent with response: ", JSON.stringify(response))
    
)

console.log("END");

结果

当我运行它时

node app.js  

我一直得到

START                                                                                 
END                                                                                   
Successfully sent with response:  "results":["error":"code":"messaging/invalid-registration-token","message":"Invalid registration token provided. Make sure it matches the registration token the client app receives from registering with FCM."],"canonicalRegistrationTokenCount":0,"failureCount":1,"successCount":0,"multicastId":7577724855311354000

如何进一步调试?

【问题讨论】:

【参考方案1】:

您的令牌有一些额外的随机字符串,例如 到: 'diWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7hdiWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7h', P>

只需从您的令牌字符串中删除:diWY78iar8s:

console.log("START");
var FCM = require('fcm-node');
var serverKey = require('/Users/bheng/Desktop/Apps/APNS/node/mhn-app-firebase-adminsdk-bs45c-5ac3770488.json')
var fcm = new FCM(serverKey)
var collapseKey = 'new_message';
var message = 
    to: 'APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7hdiWY78iar8s:APA91bHJAzXe384OEYvfk4bKsyS1NQvteph7DwG7JRIMm_HuXg8EeNllVrsSi0v9W_Gh95ezbOStp3ZWuWl0AzFKxMaCOjN81yiz7A5qhkONrd7lP2CTkUbFErw28r3ONTLvo8c8sO7h',
    data: 
        cpeMac: '000000000000',
        type: 'malware'
    ,
    notification: 
        title: 'Hello baby',
        body: 'Nice body',
        tag: collapseKey,
        icon: 'ic_notification',
        color: '#18d821',
        sound: 'default',
    ,
;


fcm.send(message, function(err, response)
    if (err) 
        console.log("Something has gone wrong!")

        console.log(JSON.stringify(err));

     else 
        console.log("Successfully sent with response: ", JSON.stringify(response))
    
)

console.log("END");

来自 FCM 的回应:

Successfully sent with response:   results: [  messageId: '0:1543448946734425%479ec0e2479ec0e2'  ],
  canonicalRegistrationTokenCount: 0,
  failureCount: 0,
  successCount: 1,
  multicastId: 6133765431734591000 

【讨论】:

以上是关于提供的注册令牌无效。确保它与客户端应用从 FCM 注册时收到的注册令牌匹配的主要内容,如果未能解决你的问题,请参考以下文章

iOS:注册令牌无效。检查令牌格式

无法获取 FCM 注册令牌

注册令牌不是有效的 FCM 注册令牌

提供的注册令牌未注册

为啥不获取 Android Firebase FCM 注册令牌?

通过 FCM 发送推送通知时何时使用 iOS 设备令牌?