-
Notifications
You must be signed in to change notification settings - Fork 2.2k
onNotificationOpened not working on Android (background/foreground) #1272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Same issue |
Hi, I am having the same issue. It triggers if the app is in the foreground. |
Hello, It trigged for me though. But it doesn't get the notification data correctly, I've got undefined if it got when it goes background
|
the same problem :( |
my problem was in this Before:
After:
i forgot about this |
@vomchik didn't work for me :( I was really hoping this was the solution. |
in my case, i was missing the notification's data setup when building the notification on foreground notification event
so, when tapping the notification |
On my side this issue is solved by doing nothing. When I tested on debug mode its not working but after I running release its working. That's weird for me too. |
Duplicate of #988 and others. Please check there and the many other duplicates of this issue for a resolution, specifically see this comment #988 (comment) for a complete reference example. Some common causes on Android are:
Loving
|
@Salakar This is not a duplicate of #988. That issue is referring to notifications not appearing in the foreground when triggered by the app itself. This issue is about the I'm sending messages via the Cloud Messaging console and they appear fine in the simulator however when pressing the notification the Also the |
@Salakar Please re-open, we are all experiencing this issue. |
@willdady sorry I actually meant this was a duplicate of #1230 - can I close this in favour of the other one? Hey @gabrielbull been a while! Small world... 😄 @yangnana11 this is correct, see the below section from the docs: If you need them then you can additionally send them as part of your data in your notification as well. |
Closing in favour of #1230 - definitely a duplicate this time 🙈 see my latest comment there as well. Thanks |
This solution doesn't work. |
My solution to this problem is pretty implementation specific, but you never know what might help. My issue was that I had a splash activity which was the actual launcher activity. To get the callbacks to fire in App.js I had to:
As soon as .MainActivity had access to the intent all of the callbacks began firing on the correct events. |
@msrivas |
Solved the issue only on background using @ZardozSpeaks approach. Under my SplashActivity.java, I added the following:
I'm still trying to find the solution for foreground notifications. |
I tried this and my onNotificationOpened() callback is firing now, but I get undefined as a body inside the notification. In my main activity I have : And in androidManifest.xml, I have set android:launchMode="singleTask" for both Main activity and Splash activity. Still no luck :( |
in our app we use MainActivity which extends SplashActivity (from react-native-navigation) which extends AppCompatActivity.
I added: notification.android.setClickAction('...'); |
Any updates on this? I am stuck here. Tried all solutions above. |
I solved this with the following files NotificationListeners.js import firebase from 'react-native-firebase';
import { ts } from './FirebaseHelpers';
// https://rnfirebase.io/docs/v5.x.x/notifications/introduction
export const notificationDisplayedListener = () =>
// app in foreground
firebase.notifications().onNotificationDisplayed(notification => {
console.log('onNotificationDisplayed');
console.log(notification);
});
export const notificationListener = () =>
// app in foreground
firebase.notifications().onNotification(notification => {
console.log('notificationListener');
console.log(notification);
const localNotification = new firebase.notifications.Notification({
sound: 'default',
show_in_foreground: true,
show_in_background: true,
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.android.setChannelId('General')
.android.setSmallIcon('@mipmap/ic_notification')
.android.setColor('#F2C94C')
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications().displayNotification(localNotification);
console.log('displayed');
firebase.notifications().removeDeliveredNotification(localNotification.notificationId);
});
export const notificationOpenedListener = () =>
// app in background
firebase.notifications().onNotificationOpened(notificationOpen => {
console.log('notificationOpenedListener');
console.log(notificationOpen);
const { action, notification } = notificationOpen;
firebase.notifications().removeDeliveredNotification(notification.notificationId);
console.log('OPEN:', notification);
});
export const notificationTokenListener = userId =>
// listens for changes to the user's notification token and updates database upon change
firebase.messaging().onTokenRefresh(notificationToken => {
console.log('notificationTokenListener');
console.log(notificationToken);
return firebase
.firestore()
.collection('users')
.doc(userId)
.update({ pushToken: notificationToken, updatedAt: ts })
.then(ref => {
console.log('savePushToken success');
})
.catch(e => {
console.error(e);
});
}); SplashActivity.java package com.daviswhitehead.shayr.android;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
// Pass along FCM messages/notifications etc.
Bundle extras = getIntent().getExtras();
if (extras != null) {
intent.putExtras(extras);
}
startActivity(intent);
finish();
}
} AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.daviswhitehead.shayr.android"
xmlns:tools="http://schemas.android.com/tools" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:name=".MainApplication"
android:label="@string/APP_NAME"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme"
tools:replace="android:label">
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:label="@string/APP_NAME"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/APP_NAME"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize"
android:exported="true"
android:launchMode="singleTask" >
</activity>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<!-- NOTIFICATION DEFAULTS -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_notification" />
<!-- android:resource="@drawable/ic_notification" /> -->
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/yellow" />
<!-- SHARE ACTIVITY -->
<activity
android:noHistory="true"
android:name=".share.ShareActivity"
android:configChanges="orientation"
android:label="@string/TITLE_ACTIVITY_SHARE"
android:screenOrientation="portrait"
android:theme="@style/Theme.Share.Transparent" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
// for sharing links include
<data android:mimeType="text/plain" />
// for sharing photos include
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<!-- FABRIC -->
<meta-data
android:name="io.fabric.ApiKey"
android:value="c12e5c4bb8cd8ca855a8ada44fa3fde413006659" />
<!-- REACT-NATIVE-FIREBASE NOTIFICATIONS -->
<service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- SCHEDULED NOTIFICATIONS -->
<receiver android:name="io.invertase.firebase.notifications.RNFirebaseNotificationReceiver"/>
<receiver android:enabled="true" android:exported="true" android:name="io.invertase.firebase.notifications.RNFirebaseNotificationsRebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
|
For those who were getting Instead of this code block below:
I used this:
Now everything works like a charm. I am using a newer firebase version btw This is my package.json below: This is my project level build.gradle dependencies below:
|
Hi guys, I am in the same situation, React native version: 0.57.0 I try everything above but no hope. Do you know any combination to make it work or I need to upgrade the version of react-native and react-native-firebase? Thank you in advance! |
I can't offer guidance on the specific issue but as regards upgrading I can think of a massive number of issues resolved between what you're running and current versions. You'll like the 'rn-diff-purge' project to take the fear out of react-native upgrades so you can get to 0.59.4 (actually 0.59.5 is releasing today I think), which will let you use react-native-firebase 5.3.1 and all the firebase SDK updates as well. Worth it I think |
thank you @mikehardy , I update the project to newest and it still undefined. However, I am able to receive the data from push notification. That is good for me |
I also ran into this problem ( |
Hi @pierregoutheraud , I set the launchMode to |
@truongluong1314520 The documentation says: "On Android, unfortunately there is no way to access the title and body of an opened remote notification. You can use the data part of the remote notification to supply this information if it's required." |
Thank you @pierregoutheraud |
in my case getInitialNotification was called but onNotificationOpened was not. #1272 (comment) but I as getInitialNotification was called always whenever the app came to foreground I used this (combined with checking if I have already processed this notifictation) to workaround this problem. I added getInitialNotification when initializing the App (to catch a cold start) and then boud it to appstate.change to call the callback when the app came back from background to foreground.
|
Have been struggling with background notitifcation no picking up in android... Got it to work finally. changed the launcher to singleTask. Thanks |
Using react-native-bootsplash which already forwards the FCM Intent. Just had to set |
Interesting @r4mdat if I were to re-do my splash screen (which I may in the future) I would use react-native-bootsplash over react-native-splash-screen - it seems to be handling the Intent correctly https://github.com/zoontek/react-native-bootsplash/blob/master/android/src/main/java/com/zoontek/rnbootsplash/RNBootSplashActivity.java#L27 which is why it doesn't have the problems splash-screen had when chaining Activities |
I came here looking for the solution of why |
onNotificationOpened can be triggered, but "react-native": "0.60.6" |
android document said "The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications." - I wonder what is "unfamiliar" behavior ? |
React-native programming model for Android is that there is only one activity and really it should always be on top and all by itself. That makes sense since really it's javascript showing "screens" inside the activity. In normal Android apps each screen is an Activity. And you might have the same screen showing different pieces of data, so two Activities on the stack with other Activities on top etc - so the normal Android app has a different Activity stack model and singleTask wouldn't fit it. |
Amazing! It works for me. Thank you! I also changed launchMode activity .MainActivity from singleTop to singleTask. |
react-native-boot-splash is the thing to use. It passes Intent data correctly and is well maintained |
@daviswhitehead thank you, it worked for me!!! |
Issue seem to be persisting and not related to react-native-splash-screen : wix/react-native-notifications#958 |
Using
|
Issue
When my app is closed and I tapped on notification getInitialNotification is working fine on both platforms, but on android when I tapped on notification and my app is in background/foreground onNotificationOpened didn't triggered. When my app in foreground I display a local notification.
Environment
React Native
version: 0.54React Native Firebase
Version: 4.0.4Firebase
Module: notificationstypescript
? noLoving
react-native-firebase
? Please consider supporting their open collective:👉 https://opencollective.com/react-native-firebase/donate
The text was updated successfully, but these errors were encountered: