'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 :
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));
},
);
}
}
Solution 1:[1]
To send push notifications to multiple devices over FCM you need to use topics and subscribe certain group of users to the same topic. 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
If you want to unsubscribe from topic you just use FCM.unsubscribeFrom()..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nebojša Smrzli? |
