How to know if the app is open from the android notification tray? For instance, I have closed the app (cleared from recent app list). but I receive notification from the backend websocket, i pressed it, it opens the app. So my question is, is there way to check if this is open from notification?
Asked
Active
Viewed 2,633 times
3
-
I've never used react native but instinct would be to use putExtra() when creating the intent and check for that. https://stackoverflow.com/questions/39351650/react-native-android-get-the-variables-from-intent – Bakon Jarser Oct 01 '18 at 19:00
-
can you show me the snippet where you configured react-native-push-notification – Haider Ali Oct 01 '18 at 20:38
-
did you tried my solution? – Haider Ali Oct 02 '18 at 10:49
2 Answers
3
Looking at the source of react-native-push-notification + next 50 lines (up to setContentIntent
) you can check for the "notification" extra in the intent.
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getBundleExtra("notification");
if(bundle != null){
//check if it is the bundle of your notification and do your thing
}
}
Otherwise you can use a Native Module approach:
When you set up the PendingIntent
that you pass into the notifications .setContentIntent()
method specify an action that you then recover in the application. Example notification:
Intent intent = new Intent(context, MyActivity.class);
intent.setAction("OPEN_MY_APP_FROM_NOTIFICATION");
NotificationCompat.Builder mNotifyBuilder = NotificationCompat.Builder(this, CHANNEL)
.setContentTitle("Title")
.setContentIntent(PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT))
mNotificationManager.notify(Notification_REQUEST_CODE,
mNotifyBuilder.build())
in MyActivity.java
public void onCreate (Bundle savedInstanceState) {
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
if(action == "OPEN_MY_APP_FROM_NOTIFICATION"){
//do whatever you have to do here
}
}
Additional info: Handling intents Creating Intents

leonardkraemer
- 6,573
- 1
- 31
- 54
-
He's using react native. There's a level or two of indirection between him and a PendingIntent – Gabe Sechan Oct 01 '18 at 19:11
-
True, but as far as I know you have to do that in a [native module](https://facebook.github.io/react-native/docs/native-modules-android). correct me if I'm wrong – leonardkraemer Oct 01 '18 at 19:15
-
Yes, but he's using a specific library to do the native module for him. This is the right answer in native code. But the answer for him is either a way to use the library to make this happen, or that he has to throw out the library and do it himself. – Gabe Sechan Oct 01 '18 at 19:17
-
I dont know all the steps he has to take in between, but I know this. Feel free to use my answer to provide the whole way. – leonardkraemer Oct 01 '18 at 19:18
-
2
Its simple, you receive notifications payload in your push notification listener
import PushNotification from 'react-native-push-notification'
configurePushNotifications = () => {
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function(token) {
console.log('PushNotification token', token)
},
onNotification is where you would receive you local or remote notification and it will be called when the user clicks on notification tray
onNotification: function(notification) {
console.log('notification received', notification)
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true,
})
}
this is how the notificaion object would look like
{
foreground: false, // BOOLEAN: If the notification was received in foreground or not
userInteraction: false, // BOOLEAN: If the notification was opened by the user from the notification area or not
message: 'My Notification Message', // STRING: The notification message
data: {}, // OBJECT: The push data
}

Haider Ali
- 1,275
- 7
- 20
-
hey there, thanks for the advice. Yup, I am using this lib. Turns out, I have to put it at App.js whenever the app is not launch. Before this, I registered the push notification in the app's home page (after login and granted credentials.). But still, i am skeptical and not so comfort with such workaround. – Fuji Oct 03 '18 at 07:02
-
Why are you skeptical, this is how it should be implemented, we have implemented the listener same as you have. BTW if the answer helped you an upvote would be a kind gesture. thanks – Haider Ali Oct 03 '18 at 11:58