带有 Capacitor 推送通知和 FCM 插件的 Ionic 5 应用程序。
import { FCM } from '@capacitor-community/fcm';
import {
ActionPerformed,
PushNotificationSchema,
PushNotifications,
Token,
} from '@capacitor/push-notifications';
为什么他们都生成不同的令牌?
对于 Capacitor/PushNotifications,以下内容生成一个令牌
PushNotifications.addListener('registration',
async (token: Token) => {
console.log('token: ' + token.value);
}
).catch(e=>{alert('reqPerm'+e)});
而对于 FCM,以下内容会生成不同的令牌
FCM.getToken()
.then(async (r) => {
console.log(`token saved ${r.token}`)
})
.catch((err) => console.warn('error saving token', err));
FCM 令牌在 iOS 上工作(iPhone 接收通知),但在 Android 上注册它时,它会显示注册令牌无效的信息。所以我必须使用来自的令牌PushNotifications.addListener对于 Android,但当它收到通知时,应用程序崩溃。
我确保 google-services.json 文件位于 android/apps 文件夹中。
是什么赋予了??有什么建议么?
是的,在新的更新中,FCM 将返回 JWT 令牌。有一个与 iOS 相同的解决方法。在获得令牌之前,您需要注册它。
getToken() {
PushNotifications.requestPermissions().then(async (permission) => {
if (permission.receive == "granted") {
// Register with Apple / Google to receive push via APNS/FCM
if (Capacitor.getPlatform() == 'ios') {
await PushNotifications.register();
PushNotifications.addListener('registration', (token: Token) => {
FCM.getToken().then((result) => {
console.log(result.token); // This is token for IOS
}).catch((err) => console.log('i am Error', err));
})
} else if (Capacitor.getPlatform() == 'android') {
await PushNotifications.register()
PushNotifications.addListener('registration', async ({ value }) => {
let androidToken = value; // this is token for Android use this token
});
}
} else {
// No permission for push granted
alert('No Permission for Notifications!')
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)