【问题标题】:Ionic + Firebase - Push notificationsIonic + Firebase - 推送通知
【发布时间】:2018-12-12 11:10:31
【问题描述】:

我有一个 ionic + firebase 聊天应用,我想在收到新消息时向用户发送通知。

在 Ionic 官方文档中,为此推荐的插件是: https://github.com/katzer/cordova-plugin-local-notifications

但正如您在存储库中看到的那样,它自去年 2 月以来一直没有更新,并且基于其中的 cmets,它似乎不适用于最新的 Android 操作系统版本。

有人知道替代方案吗?

谢谢!

【问题讨论】:

    标签: firebase ionic-framework push-notification


    【解决方案1】:

    上个月我在我的应用中实现推送通知时遇到了同样的问题。这是最好的教程:https://medium.com/@senning/push-notifications-with-ionic-and-cordova-plugin-firebase-ab0c0cad3cc0

    我将本教程定制为一个文件:messaging.service.ts

    import {Injectable} from '@angular/core';
    import {ApiService} from './api.service';
    import {AppApiResponse} from '../interfaces/interfaces'
    import {Firebase} from "@ionic-native/firebase";
    import {Platform} from "ionic-angular";
    import {AngularFirestore} from "@angular/fire/firestore";
    
    @Injectable()
    export class MessagingService {
    
      private userUid: string;
    
      constructor(private firebase: Firebase,
                  public afs: AngularFirestore,
                  public platform: Platform) {
      }
    
      initializeFirebase(userUid) {
        this.userUid = userUid;
        if (!this.platform.is("core")) {
          this.firebase.subscribe("all");
          this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS();
        }
      }
    
      initializeFirebaseAndroid() {
        this.firebase.getToken().then(token => {
          this.saveTokenToFirestore(token);
          console.log('token android= ' + JSON.stringify(token));
        });
        this.firebase.onTokenRefresh().subscribe(token => {
          this.saveTokenToFirestore(token);
          console.log('token refresh android= ' + JSON.stringify(token));
        });
        this.subscribeToPushNotifications();
      }
    
      initializeFirebaseIOS() {
        this.firebase.grantPermission()
          .then(() => {
            this.firebase.getToken().then(token => {
              this.saveTokenToFirestore(token);
              console.log('token ios= ' + JSON.stringify(token));
            });
            this.firebase.onTokenRefresh().subscribe(token => {
              this.saveTokenToFirestore(token);
              console.log('token refresh ios= ' + JSON.stringify(token));
            });
            this.subscribeToPushNotifications();
          })
          .catch((error) => {
            this.firebase.logError('push erro ios= ' + error);
          });
      }
    
      subscribeToPushNotifications() {
        this.firebase.onNotificationOpen().subscribe((response) => {
          console.log('response push= ' + JSON.stringify(response));
          if (response.tap) {
            //Received while app in background (this should be the callback when a system notification is tapped)
            //This is empty for our app since we just needed the notification to open the app
          } else {
            //received while app in foreground (show a toast)
          }
        });
      }
    
      private saveTokenToFirestore(token) {
        if (!token) return;
    
        const devicesRef = this.afs.collection('devices');
    
        const docData = {
          token,
          userId: this.userUid,
        };
    
        return devicesRef.doc(token).set(docData)
      }
    }
    

    在您的代码中使用只需包含到页面构造函数

    public msgService: MessagingService
    

    并使用:

    try {
        this.msgService.initializeFirebase(user.uid);
    } catch (error) {
        console.log('fire push erro= ' + error);
    }
    

    【讨论】:

    • 谢谢!我明天试试。它适用于最新的 IOS 操作系统版本吗?
    • 根据您的代码和教程中的代码,您订阅了通知,然后如果您在 google FCM 中创建通知,设备就可以显示它们,但是,您如何在里面创建一个用户发送新聊天消息时的项目?
    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2018-12-04
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多