此问题可以通过使用 Firebase Cloud Messaging 插件来解决。 下面是示例代码:
安装 Firebase Cloud Messaging 插件:
cordova plugin add cordova-plugin-fcm
在代码中注册并初始化 FCM:
import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
constructor(private fcm: FCM) { this.initializeApp(); }
initializeApp() { this.platform.ready().then(() => { this.fcm.subscribeToTopic('topic'); this.fcm.getToken().then(token => { console.log(token); }); this.fcm.onNotification().subscribe(data => { if (data.wasTapped) { console.log('Received in background', data); } else { console.log('Received in foreground', data); } }); this.fcm.onTokenRefresh().subscribe(token => { console.log(token); }); }); }
在 Angular 服务中发送消息:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
sendPushNotification(token: string) { const body = { notification: { title: 'Title of your push notification', body: 'Body of your push notification', click_action: '', sound: 'default', icon: '' }, to: token }; return this.http.post('https://fcm.googleapis.com/fcm/send', body, { headers: { 'Content-Type': 'application/json', Authorization: 'key=your_firebase_server_key' } }); }
这样就可以在应用内触发 FCM 推送消息并正确显示。
上一篇:Angular11传递不同于字符串类型的HttpParams
下一篇:Angular11错误:没有找到组件工厂'Component'。您是否将其添加到@NgModule.entryComponents?