Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending notification using firebase admin sdk not working

I am trying to send notification from Node.js server to an ios application. It seems working if I send notification from Firebase console, but isn't working if try from my node.js server using firebase-admin sdk.

I followed tutorial from https://firebase.google.com/docs/cloud-messaging/admin/send-messages.

One thing I do not understand is the response after sending notification seems working. I get below response.

{
    "results": [
        {
            "messageId": "0:1511109840587284%a63b4c28f9fd7ecd"
        }
    ],
    "canonicalRegistrationTokenCount": 0,
    "failureCount": 0,
    "successCount": 1,
    "multicastId": 7436388871122493000
}

Does anyone know what I am doing wrong?

-- Edit

Here is the code that sends the notification. admin is the firebase-admin instance.

router.post('/notify', (req, res) => {
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "<database>.firebaseio.com"
});

var registrationTokens = [
    'tokenFromIosApp'
];
var payload = {
    data : {
        body : 'TEST'
    }
};

admin.messaging().sendToDevice(registrationTokens, payload)
    .then((response) => {
        console.log('Sent successfully.\n');
        console.log(response);
        res.status(statusCodes.Ok);
        res.json(response);
    })
    .catch((error) => {
        console.log('Sent failed.\n');
        console.log(error);
        res.status(statusCodes.InternalServerError);
        res.json(error);
    });
});
like image 939
eChung00 Avatar asked Nov 19 '17 16:11

eChung00


People also ask

How do I send messages using Firebase admin SDK?

Using the Firebase Admin SDK or FCM app server protocols, you can build message requests and send them to these types of targets: You can send messages with a notification payload made up of predefined fields, a data payload of your own user-defined fields, or a message containing both types of payload. See Message types for more information.

How do I send push notifications using Firebase-admin?

There are multiple methods available for sending push notifications using firebase-admin. Using the Firebase Admin SDK or FCM app server protocols, you can build message requests and send them to these types of targets:

How to test notifications in Firebase using API?

This project also contains a test API which you can use to test the various notifications scenarios. First, to send HTTP calls to the firebase device group API, obviously after creating your firebase app, you should go to Firebase Console > Settings > Cloud Messaging Taband get your Project Id and Server API key.

What is Firebase ADMIN Node JS SDK?

The Firebase Admin Node.js SDK enables access to Firebase services from privileged environments (such as servers or cloud) in Node.js. I am assuming you have set up the nodejs project on your machine, so you can install it from npm using your command-line interface.


1 Answers

To send a notification, the payload must use the notification key:

var payload = {
    notification: {
        title: 'My Title',
        body : 'TEST'
    }
};
like image 85
Bob Snyder Avatar answered Oct 22 '22 03:10

Bob Snyder