On Android, the onNotification callback is not invoked for remote notfications (fcm) unless the user is tapping the notification that pops up. The behaviour is the same when the app is in both background and foreground.
My app is dependent on that the onNotification is called when the user opens up the app again, regardless if it is via tapping the notification or just opening the app, or receives the notification while the app is in foreground. It works fine on iOS.
The documentations states
When any notification is opened or received the callback onNotification is called passing an object with the notification data.
here, so I'm assuming that it is supposed to work.
I've tested and verified this behaviour on Pixel 3. Not sure about other android versions.
It's an app detached from Expo.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="host.exp.exponent">
<permission
android:name="my.app.id.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- These are required permissions to make the app run -->
<uses-permission android:name="my.app.id.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- ADD PERMISSIONS HERE -->
<!-- BEGIN OPTIONAL PERMISSIONS -->
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<!-- These require runtime permissions on M -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- END OPTIONAL PERMISSIONS -->
<!-- ADD TEST PERMISSIONS HERE -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="false" />
<uses-feature
android:name="android.software.leanback"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<!-- TEST STUFF -->
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="my.app.id.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.app.id.permission.C2D_MESSAGE" />
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- END TEST STUFF-->
<application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true">
<activity
android:name=".LauncherActivity"
android:exported="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar"></activity>
<activity
android:name=".experience.ExperienceActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:theme="@style/Theme.Exponent.Light"
android:windowSoftInputMode="adjustResize"></activity>
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:launchMode="singleTask"
android:theme="@style/Theme.Exponent.Splash"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<data android:scheme="my.app" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- ADD DETACH APP SPECIFIC INTENT FILTERS -->
</activity>
<activity
android:name=".experience.HomeActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/Theme.Exponent.Light"></activity>
<activity
android:name=".experience.TvActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@style/Theme.Exponent.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<!-- WHEN_PREPARING_SHELL_REMOVE_FROM_HERE -->
<!-- ADD DEV SETTINGS HERE -->
<activity android:name="abi32_0_0.com.facebook.react.devsupport.DevSettingsActivity" />
<!-- BEGIN_SDK_31 -->
<activity android:name="abi31_0_0.com.facebook.react.devsupport.DevSettingsActivity" />
<!-- END_SDK_31 -->
<!-- BEGIN_SDK_30 -->
<activity android:name="abi30_0_0.com.facebook.react.devsupport.DevSettingsActivity" />
<!-- END_SDK_30 -->
<!-- BEGIN_SDK_29 -->
<activity android:name="abi29_0_0.com.facebook.react.devsupport.DevSettingsActivity" />
<!-- END_SDK_29 -->
<!-- BEGIN_SDK_28 -->
<activity android:name="abi28_0_0.com.facebook.react.devsupport.DevSettingsActivity" />
<!-- END_SDK_28 -->
<!-- BEGIN_SDK_27 -->
<activity android:name="abi27_0_0.com.facebook.react.devsupport.DevSettingsActivity" />
<!-- END_SDK_27 -->
<!-- BEGIN_SDK_26 -->
<activity android:name="abi26_0_0.com.facebook.react.devsupport.DevSettingsActivity" />
<!-- END_SDK_26 -->
<!-- WHEN_PREPARING_SHELL_REMOVE_TO_HERE -->
<activity
android:name=".ExponentDevActivity"
android:exported="true"
android:label="@string/dev_activity_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.Exponent.Dark"
android:windowSoftInputMode="adjustPan"></activity>
<activity
android:name=".oauth.OAuthWebViewActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.Exponent.Light"></activity>
<activity
android:name="net.openid.appauth.RedirectUriReceiverActivity"
tools:node="replace">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:path="oauthredirect"
android:scheme="com.my.app" />
</intent-filter>
</activity>
<activity android:name=".oauth.OAuthResultActivity"></activity>
<activity android:name="abi32_0_0.expo.modules.appauth.AppAuthBrowserActivity"></activity>
<!-- START OF STRIPE SCHEMES -->
<activity
android:name="expo.modules.payments.stripe.RedirectUriReceiver"
android:exported="true"
android:launchMode="singleTask"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="expo.modules.payments.stripe.a" />
</intent-filter>
</activity>
<!-- Versioned Activity for Stripe -->
<activity
android:name="abi31_0_0.expo.modules.payments.stripe.RedirectUriReceiver"
android:exported="true"
android:launchMode="singleTask"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="abi31_0_0.expo.modules.payments.stripe" />
</intent-filter>
</activity>
<!-- END OF STRIPE SCHEMES -->
<activity android:name=".chrometabs.ChromeTabsManagerActivity"></activity>
<activity
android:name=".experience.ErrorActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.Exponent.Dark"></activity>
<activity
android:name=".experience.InfoActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.Exponent.Light"></activity>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
tools:replace="android:theme" />
<activity
android:name="com.facebook.ads.InterstitialAdActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<service
android:name=".ExponentIntentService"
android:exported="false" />
<!-- Analytics -->
<receiver
android:name="host.exp.exponent.referrer.InstallReferrerReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<!--
This crashes: https://code.google.com/p/analytics-issues/issues/detail?id=667
TODO: turn it back on when it's fixed
<service
android:name="com.google.android.gms.analytics.CampaignTrackingService"
android:enabled="true"
android:exported="false" />-->
<receiver
android:name="com.google.android.gms.analytics.AnalyticsReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.gms.analytics.AnalyticsService"
android:enabled="true"
android:exported="false" />
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name"
android:value="YOUR NOTIFICATION CHANNEL NAME"/>
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description"
android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>
<!-- Change the resource name to your App's accent color - or any other color you want -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="@android:color/holo_red_light"/>
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.vetly.vetly" />
</intent-filter>
</receiver>
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerServiceGcm"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- END my stuff test -->
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/shell_notification_icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<service
android:name=".fcm.FcmRegistrationIntentService"
android:exported="false"></service>
<!-- ImagePicker native module -->
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"></activity>
<!-- ADD FABRIC CONFIG HERE -->
<!-- BEGIN FABRIC CONFIG -->
<meta-data
android:name="io.fabric.ApiKey"
android:value="a25fafe9f9edee11a9882b32e0cd7a26df6e2c42" />
<!-- END FABRIC CONFIG -->
<!-- ADD GOOGLE MAPS CONFIG HERE -->
<!-- BEGIN GOOGLE MAPS CONFIG -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCPyKmt6in3JwAsogYikNNjor8qgU9stRQ" />
<!-- END GOOGLE MAPS CONFIG -->
<!-- ADD BRANCH CONFIG HERE -->
<!-- The Facebook SDK runs FacebookInitProvider on startup and crashes if there isn't an ID here -->
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="fb0" />
<meta-data
android:name="standaloneStripeScheme"
android:value="a" />
</application>
</manifest>
Same here
Are you making the configuration outside the react life cycle?
For me, the push notification is received when the app is in the background, but onNotification is never triggered when the app is in the foreground. Any luck guys?
For me, the push notification is received when the app is in the background, but onNotification is never triggered when the app is in the foreground. Any luck guys?
I got this working, but never had that problem. In my case, the onNotification in foreground was always being triggered, only the others (background and inactive) were tricky to make it happen.
Maybe u are modifying the onNotification when the app goes foreground, but its strange the code to be executed on foreground and background is exactly the same.
My issue was when the app is in the foreground, onNotification was never called. I just resolved this by fixing the AndroidManifest.xml to what's in the README. I was migrating from GCM to FCM and missed a few lines:
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
In my case onNotification handler isn't getting called on iOS when the app is in a killed state and I tap a notification. I did checked the setup many a times, everything seems to be in place. Not sure whats going wrong. Any help would be highly appreciated.
@nairabhijit mine case is vice versa. Namely onNotification is called only when the app is killed and when I tap on the notification to open the app. Again on IOS. Can I see your code to compare them and find out what is the difference between our codes. We can merge them and everything will be fine
Having the same issue here. The push notification needs to be tapped to get onNotification to fire when the app is in the foreground. It does get called when the push notification is tapped and the app is in the background. Anyone figure this out?
Most helpful comment
For me, the push notification is received when the app is in the background, but onNotification is never triggered when the app is in the foreground. Any luck guys?