React-native-firebase: Could not send notification

Created on 9 Oct 2018  路  27Comments  路  Source: invertase/react-native-firebase

Issue

I'm trying to display a notification to the user. Using firebase.notifications().onNotification I'm able to receive it from the Cloud Messaging but as soon as I try to use firebase.notifications() .displayNotification to display it I instantly get the 'Could not send notification'.
I've also tried to put the same exact code provided in the documentation but didn't work as well, same error.

error

this.notificationListener = 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('default-channel') // e.g. the id you chose above
                    .android.setSmallIcon('ic_notification_uaufi') // create this icon in Android Studio
                    .android.setColor('#1B7BCE') // you can set a color here
                    .android.setPriority(firebase.notifications.Android.Priority.High);

                console.log(localNotification);

                firebase.notifications()
                    .displayNotification(localNotification)
                    .catch(err => console.error(err));
            } else {
                const localNotification = new firebase.notifications.Notification()
                    .setNotificationId(notification.notificationId)
                    .setTitle(notification.title)
                    .setSubtitle(notification.subtitle)
                    .setBody(notification.body)
                    .setData(notification.data)
                    .ios.setBadge(1);

                firebase.notifications()
                    .displayNotification(localNotification)
                    .catch(err => console.error(err));
            }
            console.log('Notification received!');
        });

Environment

  1. Application Target Platform: Both
  1. Development Operating System: macOs HighSierra
  1. Build Tools: xcode 10
  1. React Native version: 0.57.0
  1. React Native Firebase Version: ^4.3.8
  1. Firebase Module: Messaging - 17.3.3
  1. Are you using typescript? No

Loving react-native-firebase? Please consider supporting them with any of the below:

Notifications

Most helpful comment

I had the same problem. What resolved it was removing this line:
.android.setSmallIcon(notification.smallIcon)
because I did not set any when I created my remote notification.

Basically you need to check the values that you pass to your localNotification object, make sure that none of them are undefined.
Hope this helps someone.

All 27 comments

I'm also getting this same issue.

This is my code and it worked. Not sure what happened but it worked. Try creating/adding a channel ID. I remember I did that first and then restart my app and it worked fine. Here is my code:

 let notification_data = JSON.parse(message._data.notification)

  const notification = new firebase.notifications.Notification()
    .setNotificationId(message._messageId)
    .setTitle(notification_data.title) 
    .setSound("default")
    .setBody(notification_data.body)

  // if android
  notification
    .android.setChannelId("your_channel_id")
    .android.setSmallIcon(notification_data.smallIcon)
    .android.setPriority(firebase.notifications.Android.Priority.High)
    .android.setColor(notification_data.color)
    .android.setVibrate(500)
    .android.setLargeIcon(notification_data.largeIcon)


  // show notification
  firebase.notifications()
    .displayNotification(notification)
    .then(x => { resolve() })
    .catch(err => reject(err));
})

@vinicarra do you found a problem ?
@prodoxx I try you solution, but sill get error Could not send notification on android.

I've managed to find the issue, basically you can't use other dependencies which creates an activity that requires to be the application's MAIN and/or LAUNCHER. React native splashscreen, for example, requires the splash screen activity to be the MAIN and LAUNCHER in AndroidManifest. That won't work, your MainActivity must be the MAIN and LAUNCHER, so if you have any other dependencies that requires these configuration rules remove them. Also make sure your MainActivity is using the proper intent-filter.

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan"
        android:launchMode="singleTop"
        android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
      </activity>

I had the same problem. What resolved it was removing this line:
.android.setSmallIcon(notification.smallIcon)
because I did not set any when I created my remote notification.

Basically you need to check the values that you pass to your localNotification object, make sure that none of them are undefined.
Hope this helps someone.

I had a similar problem - in my case the problem was that I changed the color on setColor with the format "rgba(20,30,40)" but it only accepts hex formats..

@vinicarra have you managed to use splash activity as well because whenever yo remove intent filter from splash activity to main activity evryting works fine but i also wanted to use splash activity caan you help me here ?

Hello 馃憢, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

i have same issue guys plz help me
screenshot_1548334003

Hello 馃憢, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

Is there any update on this?? I'm also facing similar issue 馃槙

I have same issue guys

Hello 馃憢, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

I am facing the same issue. can anyone help me soon ?

Hello 馃憢, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

I'm still having this issue, I've tried everything and it's not working.

I'm still having this issue, I've tried everything and it's not working.

Maybe you should see "logcat" Emulator/Device log (from Android Studio) and ignore non relevant RN exception. This log gives more info about exceptions.

Best regards.

For those still having this issue, I managed to solve it in my case by following this comment above.

Basically remove any that does not apply to you in the notificationListener e.g.

const localNotification = new firebase.notifications.Notification({
          // sound: 'sampleaudio', // <-- maybe this line
          show_in_foreground: true,
        })
        // .setSound('sampleaudio.wav') // <-- and this line
        .setNotificationId(notification.notificationId)
        .setTitle(notification.title)
        .setBody(notification.body)
        .android.setChannelId('fcm_FirebaseNotifiction_default_channel') // e.g. the id you chose above
        // .android.setSmallIcon('@drawable/ic_launcher') // <-- Or this one
        .android.setColor('#000000') // you can set a color here
        .android.setPriority(firebase.notifications.Android.Priority.High);

I'm still having this issue, @arialblack14 I've tried this but it's not working for me.

@vaibhav22c Did you remove all the non relevant lines in your case e.g. sound/icon ?

@arialblack14 yes, here my code

const localNotification = new firebase.notifications.Notification({
show_in_foreground: true,
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setBody(notification.body)
.android.setChannelId('tkkl681')
.android.setColor('#000000')
.android.setPriority(firebase.notifications.Android.Priority.High);

this code working for me

firebase.notifications().onNotification(async (notification: Notification) => {
          const newNotification = new firebase.notifications.Notification()
            .setNotificationId(new Date().getTime().toString())
            .setTitle(notification.title)
            .setSubtitle(notification.subtitle || "")
            .setBody(notification.body)
            .setData(notification.data)
          {
            Platform.OS == 'android' &&
              notification
                .android.setChannelId('####')
          }
          await firebase.notifications().displayNotification(newNotification).catch((err) => {
            console.log('err---****', err);
          });
        });

For those still having this issue, I managed to solve it in my case by following this comment above.

Basically remove any that does not apply to you in the notificationListener e.g.

const localNotification = new firebase.notifications.Notification({
          // sound: 'sampleaudio', // <-- maybe this line
          show_in_foreground: true,
        })
        // .setSound('sampleaudio.wav') // <-- and this line
        .setNotificationId(notification.notificationId)
        .setTitle(notification.title)
        .setBody(notification.body)
        .android.setChannelId('fcm_FirebaseNotifiction_default_channel') // e.g. the id you chose above
        // .android.setSmallIcon('@drawable/ic_launcher') // <-- Or this one
        .android.setColor('#000000') // you can set a color here
        .android.setPriority(firebase.notifications.Android.Priority.High);

Thanks @arialblack14

For those still having this issue, I managed to solve it in my case by following this comment above.

Basically remove any that does not apply to you in the notificationListener e.g.

const localNotification = new firebase.notifications.Notification({
          // sound: 'sampleaudio', // <-- maybe this line
          show_in_foreground: true,
        })
        // .setSound('sampleaudio.wav') // <-- and this line
        .setNotificationId(notification.notificationId)
        .setTitle(notification.title)
        .setBody(notification.body)
        .android.setChannelId('fcm_FirebaseNotifiction_default_channel') // e.g. the id you chose above
        // .android.setSmallIcon('@drawable/ic_launcher') // <-- Or this one
        .android.setColor('#000000') // you can set a color here
        .android.setPriority(firebase.notifications.Android.Priority.High);

Thanks..

I'm also having this issue. At least when you don't set an android channel, it gives the error. I took an example and even stripped it down to be like this:

```
const exampleNotifications = firebase.notifications.Notification()
.android.setChannelId('Channel ID')
.setTitle('My notification title');

firebase.notifications().displayNotification(notification);

But this also gives the vague 'could not send notification' error

In my case issue was solved by adjusting icon name with the name defined in AndroidManifest.xml. Not set small icon also causes an error though I didn't find any info about that in docs.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.company.appname">
  <application
    android:icon="@mipmap/appname_ic_launcher"
  />
</manifest>
- .android.setSmallIcon('ic_launcher')
+ .android.setSmallIcon('@mipmap/appname_ic_launcher') // same as in AndroidManifest.xml

So the working example is

const localNotification = new firebase.notifications.Notification()
  .setNotificationId('id')
  .setTitle('title')
  .setBody('body')
  .ios.setBadge(2)
  .android.setSmallIcon('@mipmap/appname_ic_launcher')
  .android.setChannelId('channelId');

firebase.notifications().displayNotification(localNotification);

Also adb logcat | grep DisplayNotificationTask helped me to identify the issue. And since "Could not send notification" error being thrown from your_app/node_modules/react-native-firebase/android/src/main/java/io/invertase/firebase/notifications/DisplayNotificationTask.java it might be helpful to check doInBackground method there to figure out cases could throw an error.

See the error log via adb logcat | grep DisplayNotification really helps. Thanks.

Was this page helpful?
0 / 5 - 0 ratings