5

I am new to Firebase and to nodejs. I am trying to send a notification from one device to another using Firebase Cloud Functions.

This is the node.js code of sending the notification:

var functions = require('firebase-functions');
var admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/sendNotification/{notificationId}')
        .onWrite(event => {

        var regToken="fYRweeL8cic:APA91bH6Q_gyKKrLL...";

        // Grab the current value of what was written to the Realtime Database.
        var eventSnapshot = event.data;

        var payload = {
            data: {
                title: eventSnapshot.child("title").val()
            }
        };

        // Set the message as high priority and have it expire after 24 hours.
        var options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
        };


    admin.messaging().sendToDevice(regToken,payload,options)
    .then(function(response){
        console.log("Successfully sent message: ", response);
    })
    .catch(function(error){
        console.log("Error sending message: ", error);
    })
})

This is the code of adding the notification to the Realtime Database in order to trigger the function:

 public void sendNotification(){
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference myRef = database.getReference("sendNotification");

        myRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Toast.makeText(getApplicationContext(),
                        "sent", Toast.LENGTH_SHORT).show();

            Map data = new HashMap();
            data.put("title", "this is my title");
            data.put("message", "this is the message");
            myRef.push().setValue(data);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

I can see that the function was executed, but with the following error:

enter image description here

The notification appears in the database: enter image description here

This is how the function appears in the console:

enter image description here

The problem is that the notification is not sent. I'm getting this: {results:[{error: [Object]}] for some reason. What can be the cause of this error?


EDIT: (Solution)

As suggested in the comments, I have used this: JSON.stringify(response) to get some more information. This was the response:

 {"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}}],"canonicalRegistrationTokenCount":0,"failureCount":1,"successCount":0,"multicastId":6051985890611026000}

The response was really clear, the token has changed. I have changed it to a valid token and it worked.

Tal Barda
  • 4,067
  • 10
  • 28
  • 53
  • That last line is not an error but a warning: https://stackoverflow.com/questions/42784135/cloud-functions-for-firebase-billing-account-not-configured. I'm not sure what is causing the failure though. – Frank van Puffelen Jul 30 '17 at 17:00
  • @FrankvanPuffelen I have edited my post. Is there any more useful information I can add? – Tal Barda Jul 30 '17 at 17:40
  • Can you try `console.log("Successfully sent message: ", JSON.stringify(response));` to get more details? – camden_kid Jul 30 '17 at 19:07
  • @camden_kid I have used this and found the answer. I edited the post with the info. You can write this as the answer if you want. Thanks. – Tal Barda Jul 30 '17 at 19:38
  • Glad you found the answer. It may be better for you to move the edit to an answer for future readers. – camden_kid Jul 30 '17 at 20:05

1 Answers1

4

As suggested in the comments, I have used this: JSON.stringify(response) to get some more information. This was the response:

 {"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}}],"canonicalRegistrationTokenCount":0,"failureCount":1,"successCount":0,"multicastId":6051985890611026000}

The response was really clear, the token has changed. I have changed it to a valid token and it worked.

Tal Barda
  • 4,067
  • 10
  • 28
  • 53
  • I got similar error message when I was registering multiple devices into a topic using subscribeToTopic and then I could send messages using topic id but I do not know if out of 20users if one's reg token is invalid , the whole system go fail and then even others too would not receive message – Amit Bravo Sep 18 '18 at 18:50