简体   繁体   English

如何使用 Ionic 向我的应用程序的所有用户推送带有 firebase FCM 的通知?

[英]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.

相关问题 在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递 - Sending FCM Push Notification in Kotlin Client App - Firebase Cloud Messaging Firebase Web 应用程序中的通知收件箱(非 FCM) - Firebase Notification Inbox in Web App (Not FCM) FCM 推送通知在使用离子电容器的 IOS 中不起作用 - FCM Push Notification are not working in IOS using Ionic Capacitor FCM iOS 推送通知在应用程序终止时捕获自定义数据? - FCM iOS push notification capture custom data when app is terminated? FCM 推送通知 - 图片不推送 - FCM push notification - image not push 应用程序未运行时目标屏幕未打开 Firebase 通知 FCM? - Target Screen not open when app is not running Firebase Notification FCM? 设备休眠时 FCM 推送通知重新唤醒应用程序无效 - FCM push notification to reawaken app not effective while device slept Flutter iOS 应用程序在前台时的 FCM 推送通知 - Flutter FCM push notification for iOS when app is in foreground 如何在列表视图中使用 fcm 显示以前的推送通知 - How to display previous push notification using fcm in list-view 如何通过令牌 ID 测试 FCM 向设备推送通知 - How to test FCM push notification to a device via a token ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM