0

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.

javacoder123
  • 193
  • 3
  • 17
  • 1
    you can call webservice from server side for send notification – oo7 Mar 20 '18 at 10:29
  • See https://stackoverflow.com/questions/37435750/how-to-send-device-to-device-messages-using-firebase-cloud-messaging, https://stackoverflow.com/a/39279716/209103 and https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html – Frank van Puffelen Mar 20 '18 at 13:32
  • if you found the answer helpful, please mark it as correct – Lucem Mar 21 '18 at 10:29

1 Answers1

1

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

Lucem
  • 2,912
  • 3
  • 20
  • 33