Mel
Mel

Reputation: 705

How to send a notification to device using firebase admin

I am adding notification support to an app. I'm using react native firebase for the react native client and am able to receive notification sent by the console.

However, when I send the notification using the firebase admin tool no notification is received.

firebase admin is initialised once on server startup

    let admin = require('firebase-admin')

    let serviceAccount = require('../../appname-firebase-admin.json')

    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: 'https://appname-12e14.firebaseio.com',
    })
    console.log('initialise firebase admin')

and then I've made this test function based on the firebase admin documentation

async function sendNotification() {
  let admin = require('firebase-admin')

  let registrationToken =
    'super-long-token-goes-here'

  let message = {
    notification: { title: 'Super title', body: 'this is a test' },
    token: registrationToken,
  }

  admin
    .messaging()
    .send(message)
    .then(response => {
      // Response is a message ID string.
      console.log('Successfully sent notification:', response)
      return { success: true }
    })
    .catch(error => {
      console.log('Error sending notification:', error)
    })

  return {
    success: true,
  }
}

this results in

Successfully sent notification: projects/appname-12e14/messages/0:1574700947563522%48f1ba652348f1ba99

with no notification on the device.

edit: I've also tried using .sendToDevice()

Upvotes: 1

Views: 855

Answers (1)

lamazing
lamazing

Reputation: 533

React-native-firebase does display the notifications in an open app, you can create a custom notification and use the component you like to show it in app. Alternatively you can use a third-party service like Notify, which has a simple API to manage them.

Upvotes: 1

Related Questions