Skip to content

Notification not showing up in foreground after triggering displayNotification() (Android) #988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
leonchabbey opened this issue Apr 17, 2018 · 17 comments

Comments

@leonchabbey
Copy link

Issue

When the app is in foreground, I'd like to show possible remote notifications.
So I use new firebase.notifications.Notification() to create a new notification when a remote notification has been received and then trigger firebase.notifications().displayNotification(); to display it as it says in the docs.
It works well on iOS but not on Android (doesn't even show up in the status bar).

componentDidMount() {
    this.notificationListener = firebase.notifications().onNotification(this._onNotification);
}

(...)

_onNotification(notif) {
    const { appState } = this.state;
    console.log(notif);

    const ts = moment().format('x');

    if (appState === "active") {
      const localNotif = new firebase.notifications.Notification()
        .setNotificationId(ts)
        .setTitle(notif._title)
        .setBody(notif._body)
        .setData(notif._data);;

      if (Platform.OS === "android") {
        localNotif.android.setChannelId(CHANNEL_ID);
      }

      firebase.notifications().displayNotification(localNotif);
    }
  }

Environment

  1. Application Target Platform: iOS, Android

  2. Development Operating System: macOS High Sierra

  3. Build Tools:

  • Xcode: 9.3, iOS: 11.3
  • Android Studio: 3.1.1, Android SDK: 26.0.0
  1. React Native version: 0.55.2

  2. RNFirebase Version: 4.0.3

  3. Firebase Module: database, auth, messaging, notifications

@leonchabbey leonchabbey changed the title Notification not showing up in foreground after triggering display() (Android) Notification not showing up in foreground after triggering displayNotification() (Android) Apr 17, 2018
@mitchellbutler
Copy link

mitchellbutler commented Apr 20, 2018

It seems as if the requirements for displaying a foreground notification are stricter on Android. I was stuck on this same issue (notifications showing only in the background) and adding sound: 'default' solved it for me.

Here is my configuration:

import React, { Component } from 'react';
import firebase from 'react-native-firebase';
import { Platform } from 'react-native';

class App extends Component {

  componentDidMount() {

    const channel = new firebase.notifications.Android.Channel(
      'channelId',
      'Channel Name',
      firebase.notifications.Android.Importance.Max
    ).setDescription('A natural description of the channel');
    firebase.notifications().android.createChannel(channel);

    // the listener returns a function you can use to unsubscribe
    this.unsubscribeFromNotificationListener = firebase.notifications().onNotification((notification) => {
      if (Platform.OS === 'android') {

        const localNotification = new firebase.notifications.Notification({
            sound: 'default',
            show_in_foreground: true,
          })
          .setNotificationId(notification.notificationId)
          .setTitle(notification.title)
          .setSubtitle(notification.subtitle)
          .setBody(notification.body)
          .setData(notification.data)
          .android.setChannelId('channelId') // e.g. the id you chose above
          .android.setSmallIcon('ic_stat_notification') // create this icon in Android Studio
          .android.setColor('#000000') // you can set a color here
          .android.setPriority(firebase.notifications.Android.Priority.High);

        firebase.notifications()
          .displayNotification(localNotification)
          .catch(err => console.error(err));

      } else if (Platform.OS === 'ios') {

        const localNotification = new firebase.notifications.Notification()
          .setNotificationId(notification.notificationId)
          .setTitle(notification.title)
          .setSubtitle(notification.subtitle)
          .setBody(notification.body)
          .setData(notification.data)
          .ios.setBadge(notification.ios.badge);

        firebase.notifications()
          .displayNotification(localNotification)
          .catch(err => console.error(err));

      }
    });

    // ...
  }

  componentWillUnmount() {
    // this is where you unsubscribe
    this.unsubscribeFromNotificationListener();
  }

  // ...
};

@chrisbianca
Copy link
Contributor

Yes, Android controls whether these notifications are shown in the foreground based on the fields that you've defined. Be aware that this may differ across different versions of Android so it's worth consulting the official documentation to find out what you need to include: https://developer.android.com/guide/topics/ui/notifiers/notifications.html

@yashojha87
Copy link

@mitchellbutler you're solution seems that it should be working on first go. but I am still not been able to show notifications when app is foreground. it is showing in console though.

@mitchellbutler
Copy link

@yashojha19, I ran into a few more issues after posting the config above. I've just updated it now with a clearer example that I can confirm is working on iOS and Android. In combination with the official docs it should be enough to get things working.

@ezekel
Copy link

ezekel commented Jun 10, 2018

@mitchellbutler can I ask what is the purpose of this code


const channel = new firebase.notifications.Android.Channel(
      'channelId',
      'Channel Name',
      firebase.notifications.Android.Importance.Max
    ).setDescription('A natural description of the channel');
    firebase.notifications().android.createChannel(channel);

suppose I want to send notification via firebase console for testing do I still need that code to be declared ?

Thank you in advance.

@mitchellbutler
Copy link

@ezekel, there is information regarding Android channels here in the official documentation.

@forestPump89
Copy link

@mitchellbutler , Hey there and thanks for helping new developers, I am trying out your code for remote push notifications . Foreground works just fine I added a console.log to verify it. But when I am on the background and I tap on the notification my app starts and nothing happens,no Console,log like expected.I am aware of the chanelId required by Android. I set the chanel Id on firebase console the same string as the one I declared on your hardcoded setup.Any ideas how i can fix this?

@mitchellbutler
Copy link

mitchellbutler commented Jun 27, 2018

@tsavlis, it sounds like you have notifications displaying properly and you want to do something when you tap one. In that case you need to read the documentation on onNotificationOpened or getInitialNotification here at the bottom of the page.

This is unrelated to the issue mentioned above and I don't want to pollute the issues for this project. If you read through the documentation and those functions are not behaving properly then I recommend searching through existing issues and opening a new one if none of them are relevant.

@forestPump89
Copy link

forestPump89 commented Jun 27, 2018 via email

@leonchabbey
Copy link
Author

Sorry I haven't come back here earlier but in my case adding a .setSound("default") made the local notification show in foreground on Android as well as on iOS.

const localNotif = new firebase.notifications.Notification()
        .setNotificationId(notif.notificationId)
        .setTitle(notif.title)
        .setBody(notif.body)
        .setSound("default");

if (Platform.OS === "android")
      localNotif
         .android.setChannelId(CHANNEL_ID)
         .android.setSmallIcon('app_icon');

@MahmoudShaeer
Copy link

MahmoudShaeer commented Jul 19, 2018

@mitchellbutler i do this but get error could not send notifications ? :\
on android

@aseemupadhyay
Copy link

aseemupadhyay commented Sep 28, 2018

@mitchellbutler Hi, i did try your snippet but apparently, it is giving me an error could not send notification. Is there anything else that is needed to be done?

const channel = new firebase.notifications.Android.Channel(
"channelId",
"Channel Name",
firebase.notifications.Android.Importance.Max
).setDescription("A natural description of the channel");
firebase.notifications().android.createChannel(channel);

firebase.notifications().onNotification(notification => {
  console.log(notification);
  const localNotification = new firebase.notifications.Notification({
    show_in_foreground: true
  })
    .setNotificationId(notification.notificationId)
    .setTitle(notification.title)
    .setSubtitle(notification.subtitle)
    .setBody(notification.body)
    .setData(notification.data)
    .setSound('default')
    .android.setChannelId("channelId") // e.g. the id you chose above
    .android.setSmallIcon("ic_stat_notification") // create this icon in Android Studio
    .android.setColor("#000000") // you can set a color here
    .android.setPriority(firebase.notifications.Android.Priority.High);

  firebase.notifications().displayNotification(localNotification);
});`

EDIT

so, apparently, you need a logo (required). otherwise your notification won't render .android.setSmallIcon("ic_stat_notification") is required and must be fulfilled

@StijnCoolen
Copy link

@mitchellbutler can you share your complete implementation? I can't get the notifications to show in the foreground. Thanks!

@miguelnfuertesc
Copy link

I disabled the sound from Firebase Cloud Mesagging, then it works! Thanks @mitchellbutler

@cuongtora1996
Copy link

@mitchellbutler work like a charm. You saved my day. Thanks

@nj-kore
Copy link

nj-kore commented May 5, 2019

I changed
firebase.notifications.Android.Importance.Max
To
firebase.notifications.Android.Importance.High
when creating the channel and it worked for me.

@bsor-dev
Copy link

bsor-dev commented Jul 3, 2020

It seems as if the requirements for displaying a foreground notification are stricter on Android. I was stuck on this same issue (notifications showing only in the background) and adding sound: 'default' solved it for me.

Here is my configuration:

...........

How to do this set up in v7? I confuse in new docs now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests