当应用在前景时,离子推动通知



我正在制作一个离子3应用程序。我希望即使应用在前景时也会出现通知。我尝试使用FCM插件,仅在应用程序在后台时才收到通知。

home.ts

import { AngularFireDatabase } from 'angularfire2/database';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import firebase from 'firebase';
declare var FCMPlugin;
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  firestore = firebase.database().ref('/pushtokens');
  firemsg = firebase.database().ref('/messages');
  constructor(public navCtrl: NavController,public afd:AngularFireDatabase) {
    this.tokensetup().then((token)=>{
      this.storeToken(token);
    })
  }
  ionViewDidLoad() {
    FCMPlugin.onNotification(function (data) {
      if (data.wasTapped) {
        //Notification was received on device tray and tapped by the user.
        alert(JSON.stringify(data));
      } else {
        //Notification was received in foreground. Maybe the user needs to be notified.
        alert(JSON.stringify(data));
      }
    });
    FCMPlugin.onTokenRefresh(function (token) {
      alert(token);
    });
  }
  tokensetup(){
    var promise = new Promise((resolve,reject)=>{
      FCMPlugin.getToken(function(token){
         resolve(token);
        },(err)=>{
          reject(err);
        });
    })
    return promise;
  }
  storeToken(token){
    this.afd.list(this.firestore).push({
       uid: firebase.auth().currentUser.uid,
       devtoken: token
    }).then(()=>{
      alert('Token stored')
    }).catch(()=>{
      alert('Token not stored');
    })
    // this.afd.list(this.firemsg).push({
    //   sendername:'adirzoari',
    //   message: 'hello for checking'
    // }).then(()=>{
    //   alert('Message stored');
    // }).catch(()=>{
    //   alert('message not stored');
    // })
  }
}

通知的功能云

  var functions = require('firebase-functions');
var admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var wrotedata;
exports.Pushtrigger = functions.database.ref('/messages/{messageId}').onWrite((event) => {
    wrotedata = event.data.val();
    admin.database().ref('/pushtokens').orderByChild('uid').once('value').then((alltokens) => {
        var rawtokens = alltokens.val();
        var tokens = [];
        processtokens(rawtokens).then((processedtokens) => {
            for (var token of processedtokens) {
                tokens.push(token.devtoken);
            }
        var payload = {
                "notification":{
                    "title":"From" + wrotedata.sendername,
                    "body":"Msg" + wrotedata.message,
                    "sound":"default",
                    },
                "data":{
                    "sendername":wrotedata.sendername,
                    "message":wrotedata.message
                }
        }
            return admin.messaging().sendToDevice(tokens, payload).then((response) => {
                console.log('Pushed notifications');
            }).catch((err) => {
                console.log(err);
            })

        })    
    })
})
function processtokens(rawtokens) {
    var promise = new Promise((resolve, reject) => {
         var processedtokens = []
    for (var token in rawtokens) {
        processedtokens.push(rawtokens[token]);
    }
    resolve(processedtokens);
    })
    return promise;    
}

仅在后台应用程序时起作用。但是,当我从应用程序退出时,它不在后台时,我没有收到任何通知。

您需要编辑FCM插件文件。我现在只为Android找到了解决方案。

我使用https://github.com/fechanique/cordova-plugin-fcm此FCM插件,用于Cordova的Android和iOS。

您需要编辑文件myfirebasemessagingservice.java Line 53(行否可能有所不同)。

在此文件中,该方法的末尾有一个方法onMessageReceived,有一行评论,该行调用另一种方法,即sendNotification(....)

sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), data);

您必须从 remoteMessage.getData() to data中取消注释并更改最后一个参数(代码中已经存在数据变量)。

并评论此行FCMPlugin.sendPushPayload( data );

现在你很好。现在,即使打开应用程序(前景),您也可以收到通知,您将收到横幅(浮动)通知。

如果您找到了iOS的任何东西,请让我知道!

我正在为离子3使用firebase插件。检查是否包含"通知数据"是否包含" Notification_foreground",并将其保存在可变的前景notification中。

  if(data.containsKey("notification_foreground")){
            foregroundNotification = true;
        }

然后,它创建 showTification 变量,该变量决定我们是否需要显示通知并将其传递给sendmessage(显示通知功能)。

  if (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title) || (data != null && !data.isEmpty())) {
        boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback() || foregroundNotification) && (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title));
        sendMessage(data, messageType, id, title, body, showNotification, sound, vibrate, light, color, icon, channelId, priority, visibility);
    }

您的有效载荷应包含Notification_foreground,Notification_Title和Notification_body。

相关内容

  • 没有找到相关文章

最新更新