• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm trying to implement push notifications in React Native with Firebase console app by following this article:

React Native: Integrating Push Notifications using FCM

In Android, I'm able to receive notifications when the app is in background but unable to receive in foreground. Using this method to receive notifications:

async createNotificationListeners() {
  /*
   * Triggered when a particular notification has been received in foreground
   * */
  this.notificationListener = firebase
    .notifications()
    .onNotification(notification => {
      console.log("fg", notification);
      const { title, body } = notification;
      this.showAlert(title, body);
    });

  /*
   * If app is in background, listen for when a notification is clicked / tapped / opened as follows:
   * */
  this.notificationOpenedListener = firebase
    .notifications()
    .onNotificationOpened(notificationOpen => {
      console.log("bg", notificationOpen);
      const { title, body } = notificationOpen.notification;
      this.showAlert(title, body);
    });

  /*
   * If app is closed, check if it was opened by a notification being clicked / tapped / opened as follows:
   * */
  const notificationOpen = await firebase
    .notifications()
    .getInitialNotification();
  if (notificationOpen) {
    console.log("bg", notificationOpen);
    const { title, body } = notificationOpen.notification;
    this.showAlert(title, body);
  }
  /*
   * Triggered for data only payload in foreground
   * */
  this.messageListener = firebase.messaging().onMessage(message => {
    //process data message
    console.log("fg", JSON.stringify(message));
  });
}

Here, firebase.notifications().onNotification or firebase.messaging().onMessage() are not being triggered at all.

In other solutions, it's because from Android 8 you need to create a channel but I can't find any option to create channel while sending notification from FCM notification composer.

Abdul,

You need to make the notification channel using firebase on the device.

const channel = new firebase.notifications.Android
  .Channel('default', 'Default Channel', firebase.notifications.Android.Importance.Max)
  .setDescription('The default notification channel.')

firebase.notifications().android.createChannel(channel)

This will create the channel, you can call it as many times as you want safely (according to the docs). Your onNotification listener looks fine, you just need to set the android channel on the notification that comes in.

  notification.android.setChannelId('default')
  • 1
Reply Report
      • 1
    • where should I create notification channel? onNotification() is still not triggered if I'm creating a channel in componentDidMount() of the base component of my app
      • 2
    • That's the right place to put your listener. If the listener isn't firing at all I would start by double checking your setup for Messaging and Notifications. Make sure your manifest file has a service that's handling the MESSAGE_EVENT.