Reputation: 193
I want to be able to send a notification to another device on android. Currently, on my app, users can upload jobs they want doing and other users can place bids on that job. The user can then accept a bid. When the user has accepted a bid a notification or message needs to be sent to the user who placed the bid. The database that I'm using is Firebase. Each user has an account which they sign in with and a unique ID.
The only things that I have found is sending notifications to my own device.
Upvotes: 0
Views: 80
Reputation: 3082
It's easy to implement custom notifications with firebase. When the bid is placed, write the user's token and the message in a node in firebase
notificationRef.push.setValue(new NotificationModel("bid accepted", firebaseUser().getToken()))
Now to send the notification, we will use firebase functions
Install Node.js
Install firebase with node
npm install -g firebase-tools
Import firebase
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
then send the notification when the notification node
in firebase
is written
exports.bidNotification = functions.database.ref('/notification/{pushId}').onWrite((event) => {
const data = event.data;
console.log('Notification received');
if(!data.changed()){
console.log('Nothing changed');
return;
}
const payLoad = {
notification:{
title: 'App name',
body: data.val().message,
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60*60
};
return admin.messaging().sendToDevice(data.val().token, payLoad, options);
});
Finally, deploy your functions on firebase using the CLI Here is more: https://firebase.google.com/docs/functions/get-started
Enjoy
Upvotes: 1