react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

Implementing HeadlessJS Task in react-native-navigation not working as expected

Open ketn opened this issue 8 years ago • 3 comments

Issue Description

I'm using react-native-firebase to receive FCM messages via a HeadlessJS task.

However, the problem I'm having is that the headlessJS performs the task when the app is in the background, but it doesn't work when the app is closed.

I suspect this is because I need to implement headlessJS task differently in react-native-navigation. What is the correct way of registering a headless task?

Steps to Reproduce / Code Snippets / Screenshots

This is how my headless task (bgMessaging.js):

import firebase from 'react-native-firebase';
import type { RemoteMessage } from 'react-native-firebase';

export default async (message: RemoteMessage) => {
    console.log("RemoteMessage", message);

    const newNotification = new firebase.notifications.Notification()
        .android.setSmallIcon(message.data.icon)
        .android.setChannelId(message.data.channel_id)
        .setNotificationId(message.messageId)
        .setTitle(message.data.title)
        .setBody(message.data.body)
        .setData({
            key1: 'value1',
            key2: 'value2',
        });
    firebase.notifications().displayNotification(newNotification);

    return Promise.resolve();
}

and this is how I've registered it in my index.js:

import { AppRegistry } from 'react-native';
import App from './app';
import bgMessaging from './bgMessaging';

const app = new App();

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);

Environment

  • React Native Navigation version: 1.1.464
  • React Native version: 0.53.3
  • Platform(s) (iOS, Android, or both?): Android
  • Device info (Simulator/Device? OS version? Debug/Release?): ONEPLUS A5010 - 8.1.0 (Debug)

ketn avatar Jun 22 '18 18:06 ketn

@Ketn if you are still having this issue, just wrap you root navigation as shown.

import {Navigation, NativeEventsReceiver} from 'react-native-navigation';
Promise.resolve(Navigation.isAppLaunched())
  .then(appLaunched => {
    if (appLaunched) {
      startApp(); 
    } else {
      new NativeEventsReceiver().appLaunched(startApp);
    }
  });

function startApp() {
  Navigation.startTabBasedApp({ ... });
}

codebymani avatar Jul 17 '18 12:07 codebymani

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest version and report back. Thank you for your contributions.

stale[bot] avatar Aug 31 '18 13:08 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Sep 07 '18 13:09 stale[bot]