Correct setup with react-native-fcm

0

I feel like the docs at react-native-fcm are a bit of a mess and I am having a hard time figuring this out.

I currently have a production app and my android users are telling me they are not receiving notifications for events where they should be. So this is stressing me out a lot right now. On iOS everything seems fine.

In the react-native-fcm example app you can see the following:

//FCM.createNotificationChannel is mandatory for Android targeting >=8. Otherwise you won't see any notification

componentDidMount() {
  FCM.createNotificationChannel({
    id: 'default',
    name: 'Default',
    description: 'used for example',
    priority: 'high'
  })
}

Do I need to call FCM.createNotificationChannel()?? I mainly use remote notifications so is this relevant in any way?

Here is MY setup:

import FCM, {
  FCMEvent,
  NotificationType,
  RemoteNotificationResult,
  WillPresentNotificationResult,
} from 'react-native-fcm';

FCM.on(FCMEvent.Notification, async (notif) => {

  // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
  FCM.presentLocalNotification({
    id: "new_message",                               // (optional for instant notification)
    title: notif.fcm.title,                          // as FCM payload
    body: notif.fcm.body,                               // as FCM payload (required)
    sound: "default",                                   // as FCM payload
    priority: "high",                                   // as FCM payload
    click_action: "com.myapp.MyCategory",               // as FCM payload - this is used as category identifier on iOS.
    badge: 10,                                          // as FCM payload IOS only, set 0 to clear badges
    number: 10,                                         // Android only
    ticker: "My Notification Ticker",                   // Android only
    auto_cancel: true,                                  // Android only (default true)
    large_icon: "ic_launcher",                           // Android only
    icon: "ic_launcher",                                // as FCM payload, you can relace this with custom icon you put in mipmap
    color: "blue",                                      // Android only
    vibrate: 300,                                       // Android only default: 300, no vibration if you pass 0
    wake_screen: true,                                  // Android only, wake up screen when notification arrives
    group: "group",                                     // Android only
    picture: "https://google.png",                      // Android only bigPicture style
    ongoing: false,                                      // Android only
    my_custom_data:'my_custom_field_value',             // extra data you want to throw
    lights: true,                                       // Android only, LED blinking (default false)
  });

  if(Platform.OS ==='ios'){
    //optional
    //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application.
    //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
    //notif._notificationType is available for iOS platfrom
    switch(notif._notificationType){
      case NotificationType.Remote:
        notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
        break;
      case NotificationType.NotificationResponse:
        notif.finish();
        break;
      case NotificationType.WillPresent:
        notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
        break;
    }
  }
});

FCM.on(FCMEvent.RefreshToken, (token) => {
    try {
      const { currentUser } = firebase.auth();

      let updates = {};
      updates[`/allUsers/serviceUsers/${currentUser.uid}/userInfo/fcmToken`] = token;

      return firebase.database().ref().update(updates).catch(err => console.log('fcm refresh error', err))
    } catch (e) {
      console.log('couldnt update fcm refresh token', e)
    }
});

const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));

class App extends Component {
  componentWillMount() {

    let config = {configgg}

    !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();

    FCM.requestPermissions();
  }

  render() {
    return (
      <Provider store={store}>
        <Router/>
      </Provider>
    );
  }
}

export default App;

I mainly use remote notifications and it is critical for my app for it to work. Is there anything I am missing in my setup?

Any hint or suggestions will help me out a lot! Thanks!

EDIT:

Found this in adb logcat when receiving a notification (that did not show up)

NotificationService: No Channel found for pkg=com.lisdoworker, channelId=null, id=0, tag=GCM-Notification:9015992, opPkg=com.lisdoworker, callingUid=10487, userId=0, incomingUserId=0, notificationUid=10487, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)

Does this have to do with FCM.createNotificationChannel()??

react-native
react-native-fcm
asked on Stack Overflow Dec 11, 2018 by Walter Monecke • edited Dec 11, 2018 by Walter Monecke

1 Answer

0

Yeah, apparently createNotificationChannel was added in version 16 to support Android 8 and it is barely documented.

answered on Stack Overflow Dec 12, 2018 by Walter Monecke

User contributions licensed under CC BY-SA 3.0