[英]Ionic push notifications with Capacitor, registering and getting token but not receiving notifications Android
我有一個帶電容器 3 的 Ionic 5 應用程序,我正在嘗試在 Android 設備上使用 Firebase 雲消息傳遞接收通知。 我遵循了配置,(我下載了谷歌 JSON 文件並正確輸入了我的應用程序 ID 名稱)並且我得到了正確的設備令牌。 一旦我的應用程序打開,我就成功獲得了令牌,沒有任何錯誤,然后我向 Firebase 測試消息發送了一個共享我的令牌的通知,通知從未到達,而且我的記錄器中也從未收到推送通知的錯誤。 這是我用於推送通知的代碼。
export class PushNotificationsService {
constructor(private readonly http: HttpClient, private notificationState: NotificationsStore) { }
public initPush() {
if (Capacitor.getPlatform() !== 'web') {
this.registerPush();
}
}
private registerPush() {
PushNotifications.requestPermissions().then(async result => {
if (result.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
console.log('granted');
await PushNotifications.register();
} else {
// Show some error
console.log('errorr');
}
});
// On success, we should be able to receive notifications
PushNotifications.addListener('registration',
(token: Token) => { (I get through this step and access the token successfully)
console.log('Push registration success, token: ' + token.value);
this.notification state.setToken(token.value);
}
);
// I never get this step error
PushNotifications.addListener('registrationError',
(error: any) => {
console.log('Error on registration: ' + JSON.stringify(error));
}
);
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotificationSchema) => {
console.log('Push received: ' + JSON.stringify(notification));
}
);
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: ActionPerformed) => {
console.log('Push action performed: ' + JSON.stringify(notification));
}
);
}
我也有這樣的電容器.config.json:
"PushNotifications": {
"presentationOptions": ["badge", "sound", "alert"]
}
此外,我檢查了我的設備和應用程序是否具有通知權限並且都啟用了。 我嘗試通過打開和關閉我的應用程序並僅在后台打開並且通知永遠不會到達來測試此問題。 它可能是什么? 有什么線索嗎? 謝謝
Android 電話請像這樣創建頻道,測試或添加控制台錯誤截圖
if (Capacitor.getPlatform() === 'android') {
PushNotifications.createChannel({
id: 'fcm_default_channel',
name: 'app name',
description: 'Show the notification if the app is open on your device',
importance: 5,
visibility: 1,
lights: true,
vibration: true,
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.