这些过去起作用了,现在不行了。我不知道他们什么时候停止工作。我正在使用电容3,最近从电容2升级。
“@电容器-社区/fcm”:"^2.0.2“、”@电容器/推送-通知“:"^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' }
我测试过的东西:
云函数代码:
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] } } }
Ionic (角)应用程序代码:
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();
它返回不工作的令牌,如下所示:eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjE6MTAxMzMxMzU1NjY5NDphbmRyb2lkOmQyODI0NGY1MWIzYTkyYTMwN2Y5NzciLCJleHAiOjE2NjM2NDI0NDIsImZpZCI6ImNhNk94R2lFU0dXYVk1RGU1UTBNTWIiLCJwcm9qZWN0TnVtYmVyIjoxMDEzMzEzNTU2Njk0fQ.AB2LPV8wRgIhAO4---Dns2vDbIdOZsamrVJ4TtEQhPK3qktfX-Q305E3AiEAo7DoZmfKha0LhDPy2QjQrbJBXyfopTnk5AgdZUsik3k
但是如果我用
PushNotifications.addListener('registration', (token: Token) => {
console.log('token.value', token.value)
});
它返回一个令牌,它工作,看起来像:ca6OxGiESGWaY5De5Q0MMb:APA91bFxBvkS_qlMo0DW9F9nVeRsffM5Llms2DZKeq_SfGJBFSyN63LjBQh_CYW7qpFQeTJnjJmDOW1eaIiOH0GTZbxktWkfj_IYrrtdwzwbGVidOEbn67KR7508Uf0kFJKg6vlVeTey
在iOS上,等待FCM.getToken()可以正常工作。
更新2 Ah在这里发现了问题:https://github.com/capacitor-community/fcm/issues/99
发布于 2022-09-13 21:07:27
原来是由于电容器2向电容器3的转变所致。
在电容3中,你需要对安卓和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
})
https://stackoverflow.com/questions/73696594
复制相似问题