我使用 FCM 插件为 Ionic 5 推送通知演示创建了一个推送演示。为此,我正在关注此 example 和此 tutorial。这是我所做的代码。
import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';
import { INotificationPayload } from 'cordova-plugin-fcm-with-dependecy-updated';
import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
public hasPermission: boolean;
public token: string;
public pushPayload: INotificationPayload;
constructor(private platform: Platform, private fcm: FCM) {
this.setupFCM();
}
private async setupFCM() {
await this.platform.ready();
console.log('FCM setup started');
if (!this.platform.is('cordova')) {
return;
}
console.log('In cordova platform');
console.log('Subscribing to token updates');
this.fcm.onTokenRefresh().subscribe((newToken) => {
this.token = newToken;
console.log('onTokenRefresh received event with: ', newToken);
});
console.log('Subscribing to new notifications');
this.fcm.onNotification().subscribe((payload) => {
this.pushPayload = payload;
console.log('onNotification received event with: ', payload);
});
this.hasPermission = await this.fcm.requestPushPermission();
console.log('requestPushPermission result: ', this.hasPermission);
this.token = await this.fcm.getToken();
console.log('getToken result: ', this.token);
this.pushPayload = await this.fcm.getInitialPushPayload();
console.log('getInitialPushPayload result: ', this.pushPayload);
}
public get pushPayloadString() {
return JSON.stringify(this.pushPayload, null, 4);
}
}
我可以在应用打开和关闭时收到推送通知,而且我在推送通知中收到图标。
请帮忙...