[英]FCM push notifications for Android not working direct to token
这些过去可以工作,现在不行。 我无法准确确定他们何时停止工作。 我正在使用电容器 3 并最近从电容器 2 升级。
“@capacitor-community/fcm”:“^2.0.2”,“@capacitor/push-notifications”:“^1.0.9”
这是我得到的错误:
Unhandled error { Error: The registration token is not a valid FCM registration token
at FirebaseMessagingError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:44:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:90:28)
at new FirebaseMessagingError (/workspace/node_modules/firebase-admin/lib/utils/error.js:256:16)
at Function.FirebaseMessagingError.fromServerError (/workspace/node_modules/firebase-admin/lib/utils/error.js:289:16)
at Object.createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:35:47)
at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:79:51
at process._tickCallback (internal/process/next_tick.js:68:7)
errorInfo:
{ code: 'messaging/invalid-argument',
message:
'The registration token is not a valid FCM registration token' },
codePrefix: 'messaging' }
我测试过的东西:
云 function 代码:
const notification: admin.messaging.Notification = {
title: title,
body: body
}
const message: admin.messaging.Message = {
notification,
token,
android:{
notification:{
sound: 'default',
icon: 'push_logo',
color: '#000000',
}
},
apns:{
payload:{
aps: {
sound: 'default'
}
}
}
}
return admin.messaging().send(message)
这创造了:
{ notification: { title: 'test', body: 'test' },
token:
'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjE6MTAxMzMxMzU1NjY5NDphbmRyb2lkOmQyODI0NGY1MWIzYTkyYTMwN2Y5NzciLCJleHAiOjE2NTk3NTExNTAsImZpZCI6ImV3YkF0c1psUm5xZ2Mzb0tQRWs0VnYiLCJwcm9qZWN0TnVtYmVyIjoxMDEzMzEzNTU2Njk0fQ.AB2LPV8wRQIgbdIAgIU76ziJc84g5gcNFNzFyid2xeDTcAywjecKFKoCIQD1KkflpXmfOSvp28XVmTtm4JtWaaVcycQRMXtKSNUM0Q',
android:
{ notification: { sound: 'default', icon: 'push_logo', color: '#000000' } },
apns: { payload: { aps: [Object] } } }
离子(角度)应用程序代码:
PushNotifications.requestPermissions().then((permission) => {
if (permission.receive === 'granted') {
PushNotifications.register().then(result => {
this.subscribeToTopic(tenant.id);
// Register for some topics here. Code removed.
this.saveFCMToken();
})
.catch(err => {
console.log('Push register error');
console.log(err)
});
private async saveFCMToken() {
try {
const result = await FCM.getToken();
const member = this.authService.getCurrentMember();
if (member) {
// Save the token against the member
await this.authService.updateMember(member.id, { fcmToken: result.token });
}
} catch(err) {
console.log('Error saving FCM token')
console.log(err)
}
}
更新我发现了问题,尽管我不知道为什么会发生这种情况。 如果我使用 Android
import { FCM } from "@capacitor-community/fcm";
const result = await FCM.getToken();
it returns a token that DOES NOT WORK and looks like this: eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjE6MTAxMzMxMzU1NjY5NDphbmRyb2lkOmQyODI0NGY1MWIzYTkyYTMwN2Y5NzciLCJleHAiOjE2NjM2NDI0NDIsImZpZCI6ImNhNk94R2lFU0dXYVk1RGU1UTBNTWIiLCJwcm9qZWN0TnVtYmVyIjoxMDEzMzEzNTU2Njk0fQ.AB2LPV8wRgIhAO4---Dns2vDbIdOZsamrVJ4TtEQhPK3qktfX-Q305E3AiEAo7DoZmfKha0LhDPy2QjQrbJBXyfopTnk5AgdZUsik3k
但是,如果我使用
PushNotifications.addListener('registration', (token: Token) => {
console.log('token.value', token.value)
});
it returns a token THAT WORKS and looks like: ca6OxGiESGWaY5De5Q0MMb:APA91bFxBvkS_qlMo0DW9F9nVeRsffM5Llms2DZKeq_SfGJBFSyN63LjBQh_CYW7qpFQeTJnjJmDOW1eaIiOH0GTZbxktWkfj_IYrrtdwzwbGVidOEbn67KR7508Uf0kFJKg6vlVeTey
在 iOS 上等待 FCM.getToken() 工作正常。
更新2啊在这里发现了问题: https://github.com/capacitor-community/fcm/issues/99
原来这是由于从电容器 2 到电容器 3 的变化。
您需要对电容器 3 中的 Android 与 iOS 使用不同的方法。
PushNotifications.addListener('registration', async ({ value }) => {
let token = value // Push token for Android
// Get FCM token instead the APN one returned by Capacitor
if (Capacitor.getPlatform() === 'ios') {
const { token: fcm_token } = await FCM.getToken()
token = fcm_token
}
// Work with FCM_TOKEN
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.