invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.71k stars 2.22k forks source link

AndroidNotification: Missing required `channelId` property #1415

Closed smemamian closed 6 years ago

smemamian commented 6 years ago

when app is foreground I get yellow error:

YellowBox.js:80 Possible Unhandled Promise Rejection (id: 0):
Error: AndroidNotification: Missing required `channelId` property
Error: AndroidNotification: Missing required `channelId` property
    at AndroidNotification.build (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:172069:17)
    at Notification.build (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:171679:75)
    at Notifications.displayNotification (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:170690:86)
    at blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:173153:53
    at EventEmitter.emit (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:162784:37)
    at blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:170658:36
    at EventEmitter.emit (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:162784:37)
    at blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:162686:30
    at RCTDeviceEventEmitter.emit (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:3478:37)
    at MessageQueue.__callFunction (blob:http://localhost:8081/a9bbbd91-a1cf-4143-8ed0-ed77cccfe5e0:2384:44)

my codes:

import firebase from 'react-native-firebase';

function displayNotificationFromCustomData(message){
  console.log('masoud masoud')
  if(message.data && message.data.title){
    let notification = new firebase.notifications.Notification();
      notification = notification
      .setTitle(message.data.title)
      .setBody(message.data.body)
      .setData(message.data)
      .setSound("default")

      notification.android.setPriority(firebase.notifications.Android.Priority.High) /// set to High
      notification.android.setChannelId("teamcide")  ///for android 8.0 and above

      const channel = new firebase.notifications.Android.Channel(
      "daily",
      "Reminders",
      firebase.notifications.Android.Importance.High
    ).setDescription("Reminds you....");

    firebase.notifications().android.createChannel(channel);
    firebase.notifications().displayNotification(notification);
  }
}

// these callback will be triggered only when app is foreground or background
export function registerAppListener(){
  console.log('Ok')
  this.notificationListener = firebase.notifications().onNotification(notification => {
    firebase.notifications().displayNotification(notification);
  })
  this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
    const notif = notificationOpen.notification;

    console.log(notif)

    setTimeout(()=>{
      alert(`User tapped notification\n${notif.notificationId}`)
    }, 500)
  });

  this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(token => {
    console.log("TOKEN (refreshUnsubscribe)", token);
  });

  this.messageListener = firebase.messaging().onMessage((message) => {
    console.log('masoud')
    console.log(message)
    displayNotificationFromCustomData(message);
  });

}
Salakar commented 6 years ago

Error: AndroidNotification: Missing requiredchannelIdproperty - your notification is missing a channelId like the error says 😋

Also firebase.notifications().android.createChannel(channel); is a promise so it's asynchronous. You need to await it before creating your notification.


Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:

achmadk commented 6 years ago

According to the docs, firebase.notifications().android.createChannel(channel); is still not a asynchronous. Maybe Mr. @Salakar can give an example of working code.

quangvietntd commented 6 years ago

channelId does not match! notification.android.setChannelId("teamcide") --> notification.android.setChannelId("daily")