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.

Can't access notification data when the app is in background or killed

See original GitHub issue

Hi!

First of all, great work with this library!

react-native@0.48.0
react-native-fcm@10.0.2
OS: Android - Emulator - 7.0.2

When my app is in foreground, its working perfectly, I receive the data in this function

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

And I can handle everything

image

But when the app is in background, the notification is received, but when I tap the banner, the app goes foreground, the same function is triggered, but the notif object is different, and don’t contains my data.

image

When the app is killed, also the notification is received, but when I tap the banner, the app starts, the FCM.getInitialNotification() is triggered, and my notif object doen’t also contain the data, the same behaviour than in background

image

I am testing the format of the messages using node-gcm library, of firebase web interface, but nothing works.

This is a sample of my message:

let message = new gcm.Message({
	priority: 'high',
	content_available: true,
	timeToLive: 3,
	data: {
		key1: 'message1',
		key2: 'message2'
	},
	notification: {
		title: "Hello, World",
		icon: "ic_launcher",
		body: "This is a notification that will be displayed if your app is in the background."
	},
	custom_notification: {
		key1: 'message1',
		key2: 'message2'
	}
	
});

How should I proceed to send and receive data in those cases? Because is driving me crazy.

If you need more data, ask for it 😃

Thanks!!

EDIT:

I have two activities in my native part, as I am handling a splash screen in the native way. can this be causing this effect?

This is my manifest.xml. I don’t want to handle local notifications, only remotes

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.myapp"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="22" />

    <application
            android:name=".MainApplication"
            android:allowBackup="true"
            android:label="@string/app_name"
            android:icon="@mipmap/ic_launcher"
            android:theme="@style/AppTheme">
        <meta-data
                android:name="com.google.firebase.messaging.default_notification_icon"
                android:resource="@drawable/ic_stat_azvase_notification" />
        <service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <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=".SplashActivity"
                android:label="@string/app_name"
                android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"
                  android:launchMode="singleTop"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                  android:windowSoftInputMode="adjustResize"
                  android:exported="true"

        >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

    </application>

</manifest>

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
evollucommented, Oct 26, 2017

issue 2: try remove the notification part and add "content_available": true. See if the notification gets delivered. issue 3: App should wake up after reboot or system kill because of memory shortage. Swiping away in task center will prevent app waking up. System behavior.

I would not rely on notification and sync data with server on launch for both Android and iOS because user can turned off notification.

0reactions
balapkmcommented, May 29, 2018

@evollu still i’m getting below msg only in android if i opened app .How to fix this

{
  "content_available": false,
   "notification": {
       "title": "hello",
       "body": "yo"
   },
   "data": {
       "extra":"juice"
   },
  "to":"cbGVdEcQcl5pXvT8wBqo1e8I_nALw7czQaymhDRa1ol0IZD4KV2GVqmDANy6_oJcphyIizEkF9LLtyf18FnYXvd6buARv60y8MLlGds-jpQUvqOPs7Xiat0DV-HiPwmNSPRmZAbn"
}

{ opened_from_tray: 1, fcm: { action: ‘android.intent.action.MAIN’ } }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to extract data from FCM notification when app is in ...
Able to receive data from push notifications when application is in foreground. But not on background/ kill. Receiving and posting data to ...
Read more >
onNotification not being triggered when app is killed #1555
In my Android logs I get Cannot send to notification centre because there is no 'message' field in: Bundle[{userInteraction=false, id=707734631, ...
Read more >
Notifications Not Shown - Mobile Push
MIUI 10, Settings > Battery & performance > Choose apps > Select the app that is not showing notifications > Select No restrictions....
Read more >
React-native FCM: Can't access notification data when the ...
Coding example for the question React-native FCM: Can't access notification data when the app is in background or killed-React Native.
Read more >
Why my push notifications are not being received?
To access this notification data you received while the app is in the background/killed, all that you have to do is… Here comes...
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