Flutter: Firebase Pushnotification On Data Change

痴心易碎 提交于 2019-12-11 12:17:32

问题


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!!


回答1:


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



来源:https://stackoverflow.com/questions/50663590/flutter-firebase-pushnotification-on-data-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!