question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

background app - on("notification") not triggered after notification click

See original GitHub issue

Versions: rn - 0.34.1 fcm - 2.3.2

I have an android app. When I click on home button to move the app to background and then sending a notification, the notification goes to notification area, but when I click on the notification, the app goes to foreground directly (it’s not re-launching) and the notification data is not passed to the app. Is this related to the hybrid notification problems? I thought that the hybrid problem is just when not clicking on the notification, but when clicking it should pass the data to the app somehow.

notification payload:

{
        priority: 'normal',
        contentAvailable: true, //for ios
        delayWhileIdle: true,
        timeToLive: fcmConfig.timeToLive,
        restrictedPackageName: 'my app id',
        dryRun: fcmConfig.isDryRun,
        data: {
            myData: {
                id: '123456',
            }
        },
        notification: {
            title: 'Hello world title',
            icon: "ic_launcher",
            body: 'Hello world body',
            sound: 'default',
        },
    }

javascript:


///When app started:
FCM.getInitialNotification()
        .then(notification => {
            if (notification && notification.myData) {
                alert(notification.myData);
            }
        });
FCM.on('notification', (notif) => {
        alert("on: " + notif.myData);
});

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19

github_iconTop GitHub Comments

14reactions
dsloungecommented, Oct 18, 2016

I didn’t need to add an AppState listener whatsoever. To summarize, this is how I’m handling notifications in my app, there’s 3 scenarios:

App isn’t Running:

  • Firebase SDK will display a system notification. Pressing the banner will open the app, and getInitialNotification() will fire. This is a promise that seems to always fire (with an empty notification if there isn’t an initial notification), so you can show a loading screen and wait until this promise resolves to show a screen based on the notification

App is in Foreground:

  • Firebase SDK wont fire a system notification. Instead, the data payload will get passed through FCM.on('notification' event. If you want to show a system notification, here you need to trigger a manual, local notification. Be careful with this, since the local notification will fire FCM.on('notification' itself, and you could have a loop creating infinite notifications.

App is in Background:

  • Firebase SDK will display a system notification. Pressing the banner will open the app with an intent, specified by the click_action. The activity needs to be specified as singleTop and be able to handle that intent. The Activity will show and because it’s already running, FCM.on('notification' event will fire.

Hopefully that’s helpful. That’s been my experience with this package, and I’m running Android Marshmallow. It might be different on other phones.

To be honest with you, I think the right thing todo is to write my own Firebase listening service, use just data notifications, and fire local notifications when those are received. That’s what I’m going to do eventually. Getting all this to work was a big pain, and it’s mostly because of Firebase.

More notes…

  • I didn’t change MainActivity.java
  • using "react-native": "0.33.0", "react-native-fcm": "2.3.0",

Sample payload:

{
  "to" : "token-goes-here",
  "notification" : {
    "title" : "Story title",
    "body" : "location name",
    "icon" : "ic_stat_notify",
    "sound": "default",
    "click_action": "geosocial.story"
  },
  "data" : {
    "title" : "Story title",
    "body" : "location name",
    "icon" : "ic_stat_notify",
    "story_id" : "10279"
  },
}

Relevant parts of AndroidManifest (notice geosocial.story intent)

<application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher_geosocial"
      android:theme="@style/AppTheme">

      <!-- For displaying local notifications -->
      <receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>

      <!-- For listening to firebase messages -->
      <service android:name="com.evollu.react.fcm.MessagingService">
        <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
      </service>

      <!-- For handling rotating firebase ids -->
      <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
        <intent-filter>
          <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
      </service>

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
          <action android:name="geosocial.story" />
          <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_api_key"/>
    </application>
5reactions
foggy1commented, May 16, 2017

@atlanteh @dslounge @kylebebak Do any of you still use this library? I’m completely mystified. Despite specifying click_action and having the matching intent, no combination of notification and data currently fires off the main callback at all on android, and I have no idea how to even go about debugging it. My app is receiving notifications in the background, but opening it from tray never, ever gets recognized.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - onMessage is not triggered When background push ...
onMessage function I've written in App.js file. My requirement is onMessage function should trigger after click on background notification from ...
Read more >
Notifications Not Shown - Mobile Push
The following are reasons why notifications may show as "Delivered" on the OneSignal dashboard or API, but are not visible on your device....
Read more >
Understanding message delivery | Firebase Cloud ... - Google
Opens — The user opened the notification message. Reported only for notifications received when the app is in the background. This data is...
Read more >
didReceiveRemoteNotification not c… - Apple Developer
From the docs it states that this method is called even when the app is in background: Use this method to process incoming...
Read more >
Restrictions on starting activities from the background
The app receives a notification PendingIntent from the system. In the case of pending intents for services and broadcast receivers, the app can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found