0

In my android application, I want to send notifications for text messages send from one user to another and I've deployed this Node.js function into firebase functions:

'use strict'


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


exports.sendNotification = functions.database.ref(`/notification/{receiver_user_id}/{notification_id}`)
.onWrite((data, context) =>
{
    const receiver_user_id = context.params.receiver_user_id;
    const notification_id = context.params.notification_id;


    console.log('We have a notification to send to :' , receiver_user_id);


    if (!data.after.val()) 
    {
        console.log('A notification has been deleted :' , notification_id);
        return null;
    }

    const DeviceToken = admin.database().ref(`/users/${receiver_user_id}/user_id`).once('value');

    return DeviceToken.then(result => 
    {
        const token_id = result.val();
        console.log("Token Id ", token_id);

        const payload = 
        {
            notification:
            {
                title: "New Mesaage",
                body: `you have a new Message, Please Check.`,
                icon: "default"
            }
        };

        console.log("This is payload ", payload);

        return admin.messaging().sendToDevice(token_id, payload).then()
            .catch(function (error) 
                {
                    console.log("Error sending message: ", error);  // here no return
                });

    });
});

I think the problem lies with sendToDevice() method as I'm not getting any notification and I don't want to send by device_token.

I want to send notifications to the device in which the user with a particular "user_id" is logged in This is my database model

This is the log I got:

  • Firebase Cloud Messaging only knows about devices/app instances. It has no knowledge of a user. If you want to associate users with devices/app instances, you'll have to do this in your own code. – Frank van Puffelen Aug 30 '20 at 15:33
  • Then what will happen if same user is logged in on multiple devices.? – Divya Vyas Aug 30 '20 at 16:21
  • You can try https://ravenapp.dev . You can setup your FCM keys in the dashboard, and call its api to send a notification. It will manage your device tokens for you. – proy31 Sep 02 '20 at 10:29

1 Answers1

0

and I don't want to send by device_token

If you want to target a message to a particular user's device, you must use a device token. That's the way it works.

FCM doesn't know anything about the individual users of your app. FCM just knows about individual devices whose tokens you collect in the app and send to your backend (or store in your database). You have to associate the token to the user account somehow. You should also assume that one user might be using multiple devices.

What you should probably do first is start collecting the device tokens and storing them in a field under your user data. Then, when you query that user, you can find the device tokens to use to send the message.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441