【问题标题】:FCM Push Notification are not working in IOS using Ionic Capacitor使用离子电容器的 FCM 推送通知在 IOS 中不起作用
【发布时间】:2021-11-02 09:19:45
【问题描述】:
使用 CAPACITOR 和 IONIC 在 Android 上一切正常,但在 IOS 中出现以下错误:
在检索发件人 ID“mySenderId”的 FCM 令牌之前未设置 APNS 设备令牌。此 FCM 令牌的通知将不会通过 APNS 传递。请确保在设置 APNS 设备令牌后重新检索 FCM 令牌。
???? Capacitor Doctor ????
Latest Dependencies:
@capacitor/cli: 3.2.2
@capacitor/core: 3.2.2
@capacitor/android: 3.2.2
@capacitor/ios: 3.2.2
Installed Dependencies:
@capacitor/cli: 3.2.2
@capacitor/core: 3.2.2
@capacitor/android: 3.2.2
@capacitor/ios: 3.2.2
我可以在我的Xcode 日志中看到 FCM 令牌,但通知不起作用。
参考资料:
My Issue
Simmilar Issue
【问题讨论】:
标签:
ios
firebase
ionic-framework
firebase-cloud-messaging
capacitor
【解决方案1】:
解决办法是在FCM token之前注册APNS token。
PushNotifications.requestPermissions().then((permission) => {
if (permission.receive == "granted") {
// Register with Apple / Google to receive push via APNS/FCM
if(Capacitor.getPlatform() == 'ios'){
PushNotifications.register().then((res)=>{
console.log('From Regisiter Promise', res)
})
PushNotifications.addListener('registration', (token: Token)=>{
FCM.getToken().then((result) => {
this.remoteToken = result.token;
}).catch((err) => console.log('i am Error' , err));
})
}else{
FCM.getToken().then((result) => {
this.remoteToken = result.token;
}).catch((err) => console.log('i am Error' , err));
}
} else {
// No permission for push granted
alert('No Permission for Notifications!')
}
});
对于最新版本的插件:
PushNotifications.requestPermissions().then((permission) => {
if (permission.receive == "granted") {
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
console.log(token);
})
} else {
// No permission for push granted
alert('No Permission for Notifications!')
}
});