Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Function executed successfully but notification not showing android

In an android app, I am using FCM for sending notifications, the cloud function executed successfully, as shown in the firebase console log, but in my device its not showing any notification, what could be the reason ?

Below is the code for my index.js

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

exports.sendNotification = functions.database.ref('/notifications/messages/{pushId}')
.onWrite(event => {
     console.log('notifying start1');
    const message = event.data.current.val();
    const senderUid = message.from;
    const receiverUid = message.to;
    console.log('SenderId '+senderUid + ' Receiver Id '+receiverUid);
    const promises = [];
 console.log('notifying start2');
    if (senderUid == receiverUid) {
        //if sender is receiver, don't send notification
        promises.push(event.data.current.ref.remove());
        return Promise.all(promises);
    }
      console.log('notifying start3');
    const getInstanceIdPromise = admin.database().ref(`/users/${receiverUid}/accessToken`).once('value');
      console.log('notifying start4');
    const getReceiverUidPromise = admin.auth().getUser(receiverUid);
console.log('notifying start5');
    return Promise.all([getInstanceIdPromise, getReceiverUidPromise]).then(results => {
        const accessToken = results[0].val();
        const receiver = results[1];
        console.log('notifying ' + receiverUid + ' about ' + message.body + ' from ' + senderUid);
        const payload = {
            notification: {
                title: 'Firebase Notification',
                body: message.body,
            }
        };
        admin.messaging().sendToDevice(accessToken, payload)
            .then(function (response) {
                console.log("Successfully sent message:", response);
            })
            .catch(function (error) {
                console.log("Error sending message:", error);
            });   
            });
            });

Kindly help! Thanks in advance.

like image 947
sara Avatar asked Jul 04 '17 09:07

sara


People also ask

How do I send notifications from cloud function?

In order to implement the push notification functionality you need to connect Firebase Cloud Messaging to the app. The easiest way to do this is to use the Firebase Assistant from the Tools menu in Android Studio. In the assistant, select Cloud Messaging and follow the steps on screen.

What is the cloud notification on an android?

Google Cloud Messaging (GCM) was a mobile notification service developed by Google that enables third-party application developers to send notification data or information from developer-run servers to applications that target the Google Android Operating System, as well as applications or extensions developed for the ...

How do I get notification data when an app is in the background?

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).


Video Answer


1 Answers

Had the same problem and couldn't understand what was wrong since the error details were not shown i.e. { error: [Object] }

Successfully sent message: { results: [ { error: [Object] } ], 
                             canonicalRegistrationTokenCount: 0, 
                             failureCount: 1, 
                             successCount: 0, 
                             multicastId: 5487635521698134000 
                           }

So changed/added a log in the cloud function code to access the error details i.e. console.log(response.results[0].error);.

code (in the cloud function):

admin.messaging().sendToDevice(registrationToken, payload)
            .then(function(response) {
                console.log("Successfully sent message:", response);
                console.log(response.results[0].error);
            })
            .catch(function(error) {
                console.log("Error sending message:", error);
            });

error details:

{ Error: 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.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:130:23)
at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:154:16)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:80:63
at Array.forEach (native)
at mapRawResponseToDevicesResponse (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:76:26)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:223:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

errorInfo: 
   { 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.' } }

Not sure if you have the same error I do...

like image 74
bibangamba Avatar answered Oct 12 '22 22:10

bibangamba