Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error after trying to send notification through Firebase Cloud Functions (Android)

Tags:

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.

like image 550
Tal Barda Avatar asked Jul 30 '17 16:07

Tal Barda


1 Answers

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.

like image 179
Tal Barda Avatar answered Sep 30 '22 15:09

Tal Barda