[英]How to push notification with firebase FCM to all users of my App with Ionic?
I need to notify all users with latest news, i did only notification method with token of user but notification shown only on the device of this user,i need to notify all users of my application, this code is for send notification by token of user only, how to send to group of users or all users, thanks for help我需要通知所有用户最新消息,我只使用用户令牌通知方法但通知只显示在该用户的设备上,我需要通知我的应用程序的所有用户,此代码用于通过用户令牌发送通知只有,如何发送给用户组或所有用户,感谢帮助
app.component.ts:应用程序组件.ts:
import { Component, OnInit } from '@angular/core';
import { CrudService } from './crud.service';
import { Platform } from '@ionic/angular';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Router } from '@angular/router';
import {
ActionPerformed,
PushNotificationSchema,
PushNotifications,
Token,
} from '@capacitor/push-notifications';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
})
export class AppComponent implements OnInit {
constructor(public crud: CrudService , private platform: Platform,
private statusBar: StatusBar,
public router:Router){
this.initializeApp();
crud.databaseConn();
crud.getUsersList();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.router.navigateByUrl('splash');
});
}
ngOnInit() {
console.log('Initializing HomePage');
// Request permission to use push notifications
// iOS will prompt user and return if they granted permission or not
// Android will just grant without prompting
PushNotifications.requestPermissions().then(result => {
if (result.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
PushNotifications.addListener('registration', (token: Token) => {
alert('Push registration success, token: ' + token.value);
console.log("token"+token.value)
});
PushNotifications.addListener('registrationError', (error: any) => {
alert('Error on registration: ' + JSON.stringify(error));
});
PushNotifications.addListener(
'pushNotificationReceived',
(notification: PushNotificationSchema) => {
var audio = new Audio("assets/bip_bip.mp3");
audio.play();
alert('Push received: ' + JSON.stringify(notification));
},
);
PushNotifications.addListener(
'pushNotificationActionPerformed',
(notification: ActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
},
);
}
}
To send push notifications to multiple devices over FCM you need to use topics and subscribe certain group of users to the same topic.要通过 FCM 向多个设备发送推送通知,您需要使用主题并为特定的用户组订阅同一主题。 For example:例如:
import { PushNotifications } from "@capacitor/push-notifications";
import { FCM } from "@capacitor-community/fcm";
Import { Capacitor } from “@capacitor/core”;
// set up base push notifications with Capacitor
await PushNotifications.requestPermissions();
await PushNotifications.register();
let topic;
if (Capacitor.getPlatform() === ‘ios’) {
topic = ‘apple’
} else if (Capacitor.getPlatform() === ‘android’) {
topic = ‘google’
}
// set up Firebase Cloud Messaging topics
FCM.subscribeTo({ topic })
.then(r => console.log(`subscribed to topic “${topic}”`))
.catch(err => console.log(err));
Then on FCM console you can test it out with sending notification to those topics: Like shown on this image然后在 FCM 控制台上,您可以通过向这些主题发送通知来对其进行测试:如图所示
If you want to unsubscribe from topic you just use FCM.unsubscribeFrom()..如果您想取消订阅主题,只需使用 FCM.unsubscribeFrom()..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.