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()??

Walter Monecke
  • 2,386
  • 1
  • 19
  • 52
  • Have you added release SHA-1 to the fcm console? if not try this https://stackoverflow.com/questions/51612535/react-native-google-signin-fail-when-i-will-build-release-apk/51613254#51613254 – Bhagwat K Dec 11 '18 at 18:43
  • I think you dont need SHA-1 for GCM. Right? @Cool7 – Walter Monecke Dec 11 '18 at 18:57
  • I think we need it when we are using fcm. – Bhagwat K Dec 11 '18 at 19:03
  • I added it to firebase console in settings. Will try it out. – Walter Monecke Dec 11 '18 at 19:13
  • @Cool7 Still not working :/ – Walter Monecke Dec 11 '18 at 19:58
  • You should better use react-native-firebase if you are using firebase for sending notifications. That lib offers a better way to do that. You need to keep in mind that for push-notifications you need to join to apple developer program – Helmer Barcos Dec 11 '18 at 20:23
  • @HelmerBarcos yes I know, I already implemented this lib. I wanted to see if there was any way to work with it. – Walter Monecke Dec 11 '18 at 20:34
  • Then use the cloud Messaging module. It gives you all you need. on Github from react-native-fsm they say _react-native-firebase has introduced new firebase messaging and remote/local notification features. It has good integration with other firebase features. I would recommend new comers to check that._ – Helmer Barcos Dec 11 '18 at 20:37
  • Yes I know but it is still GCM. So react-native-fcm should still work. Try keeping it to this module please. If I cant find an answer I will look into other solutions. – Walter Monecke Dec 11 '18 at 20:43

1 Answers1

0

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

Walter Monecke
  • 2,386
  • 1
  • 19
  • 52