Send push notifications with firebase-admin from nodejs

I have a nodejs project from which I need to send push notifications to devices. I have the app configured in firebase and sending test messages from firebase console works fine. But from my code in nodejs its not working but the response message is success.

RESPONSE

 Successfully sent notification
 {
   responses: [
    {
      success: true,
  messageId: 'projects/bioter-app/messages/0:1661888947319442%2fcd2f4df9fd7ecd'
     }
   ],
  successCount: 1,
   failureCount: 0
 }

CODE TO SEND MESSAGES

import { initializeApp, cert } from "firebase-admin/app";
import { messaging } from "firebase-admin";

import path from "path";

export interface IPushNotificacion {
    token: string,
    data: {
        title: string,
        message: string
    }
}

initializeApp({
    credential: cert(path.join(__dirname, 'keys', 'bioter-app-firebase-adminsdk-11lci-9e163aab29.json'))
})

export const sendMessage = (messages: IPushNotificacion[]) => {
    if (!messages.length) return
    messaging().sendAll(messages)
        .then(response => {
            console.log(`Successfully sent notification`)
            console.log(response)
        })
        .catch(err => {
            console.log('Notification could not be sent ')
            console.log(err)
        })
}

I tried reading firebase admin docs for cloud messages but i wasnt able to identify my problem. https://firebase.google.com/docs/cloud-messaging/server

Upvotes: 1

Views: 7411

Answers (2)

Salvino D'sa
Salvino D'sa

Reputation: 4506

An abstract from the Firebase docs states that:

With FCM, you can send two types of messages to clients:

  1. Notification messages: Sometimes thought of as display messages. These are handled by the FCM SDK automatically.
  2. Data messages: Which are handled by the client app and need explicit implementation of code to handle the received payload.

Now, looking at your code implementation above:

export interface IPushNotificacion {
    token: string,
    data: {
        title: string,
        message: string
    }
}

It's clear that you tried sending a data message rather than a notification message and hence firebase-admin returned you a success message.

That being said, if you hadn't explicitly defined code on your client app to handle the received data message, the app wouldn't know what to do with that message. Naturally producing no result/no display of notification.

Therefore, once you changed the structure to:

export interface IPushNotificacion {
    token: string,
    notification: {
        title: string,
        body: string
    }
}

firebase-admin sent a regular notification to your app and your app displayed directly without any issues.

Upvotes: 2

Well after a lossing my mind for a lot of time I found that the

messaging().sendAll() function expects a token and a notification propert with childs as title and body.

If you pass a token and another porperty, in my case data, it returns success but dont deliver the notification.

Upvotes: 2

Related Questions