eChung00
eChung00

Reputation: 631

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);
    });
});

Upvotes: 8

Views: 19282

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38309

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

var payload = {
    notification: {
        title: 'My Title',
        body : 'TEST'
    }
};

Upvotes: 17

Related Questions