Flutter:有关数据更改的Firebase Pushnotification [英] Flutter: Firebase Pushnotification On Data Change

查看:119
本文介绍了Flutter:有关数据更改的Firebase Pushnotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

收到评论后,我已将以下代码部署到我的Firebase项目中,并已成功部署!!但是没有通知发送给我。
请检查我的Firebase Realtime数据库屏幕快照,以更好地理解。



[现在解决:它将通知仅发送一个ID,即我的管理设备]



工作代码:

  const函数= require('firebase-functions'); 
const admin = require('firebase-admin');
admin.initializeApp(functions.config()。firbase);
exports.codeformypeople = functions.database.ref('items / {id}')。onWrite(evt => {

const有效载荷= {
通知:{标题:请求新客户,正文:触摸以打开应用程序,徽章: 1,声音:默认,}
};
const令牌= Lsn-bHfBWC6igTfWQ1-h7GoFMxaDWayKIpWCrzC ; //用ur令牌替换

if(令牌){
console.log('Toke is availabel。');
返回admin.messaging()。sendToDevice(token ,有效负载);
} else {
console.log('令牌错误');
}
});

[



请参见



您已经完美部署了功能代码,但是您忘记在数据库中添加刷新tokenId,如上图所示,我正在保存我的数据库和函数 admin.messaging()。sendToDevice(request.userTokenId,payload)中的userTokenId 我正在使用该tokenId,此tokenId为用于将通知发送到特定设备,您可以使用 FirebaseInstanceId.getInstance()。getToken()获取此tokenId,并将其保存在您的 fbproject1>中。 items 数据库结构。请参阅&


After getting the comment, i have deployed this folowing code to my firebase project and it was successfully deploed!.But there is no notifications been send to me. Please check my Firebase Realtime database Screenshot here for better understanding.

[ITS SOLVED NOW:IT WILL SEND NOTIFICATIONS TO ONLY ONE ID ie My Admin Device]

WORKING CODE:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firbase);
exports.codeformypeople = functions.database.ref('items/{id}').onWrite(evt => {

const payload = {
notification: { title: 'New Customer Requested', body: 'Touch to Open The App', badge: '1', sound: 'default', }
};
const token ="Lsn-bHfBWC6igTfWQ1-h7GoFMxaDWayKIpWCrzC";//replace with ur token

if (token) {
console.log('Toke is availabel .');
return admin.messaging().sendToDevice(token, payload);
} else {
console.log('token error');
}
});

[

SEE THIS VIDEO LINK FOR MORE DETAILS

note:If your app is opened and minmized then it will show notification,but if the app is opened and you are using,or if the app is terminated force close then it will not work!!

解决方案

You can use firebase cloud function to trigger notification. Here is snippet of cloud functions which i am using to trigger notification:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.pushNotification = functions.database.ref('/Notifications/{pushId}')
 .onWrite(( change,context) => {
    console.log("Push Notification event triggered");
    var request = change.after.val();
    var payload = {
        data:{
            username: request.userName,
        }
    };
    admin.messaging().sendToDevice(request.userTokenId, payload)
    .then(function(response){
        console.log("Successfully sent message: ",response);
        console.log(response.results[0].error);
    })
    .catch(function(error){
        console.log("Error sending message: ", error)
    })
 })

Below i have provided my notification structure, you can see below.This function will trigger if any new data is being pushed in database notification node. To see what is output/error you are getting when this function is trigger go to firebase admin panel > Functions > Logs.

You have deployed function code perfectly, but you forgot to add refresh tokenId in your database as you can see in above picture i am saving userTokenId in my database and in function admin.messaging().sendToDevice(request.userTokenId, payload) i am using that tokenId, this tokenId is used to send notification to particular device, you can get this tokenId using FirebaseInstanceId.getInstance().getToken() and save this in your fbproject1 > items database sturcture. please refer this & this

这篇关于Flutter:有关数据更改的Firebase Pushnotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆