React-native-push-notification: onNotification do not fired when click on received push. Android

Created on 13 Sep 2018  路  13Comments  路  Source: zo0r/react-native-push-notification

Hello!
"react-native": "0.56.0"
"react-native-push-notification": "^3.1.1"
I configured all as in example. IOS works perfectly in any situation.
Android receive push in background and in foreground.
In foreground Android fires onNotification normally.
But if I receive push on Android in background, then click on push notification, it just open my app, and do not fired onNotification event.
Is there any way to get notification data by clicking push on Android?

Most helpful comment

I wrestled with this issue for several hours the other day, and the thing that ultimately fixed it was changing my message payload object to only contain a data key - not notification or anything else.

More details in this comment: https://github.com/zo0r/react-native-push-notification/issues/740#issuecomment-431644518

All 13 comments

I am facing same issue. Are you able to solve this problem?

@bhushansethi still no

I am also facing this issue.

I am have same issue. I'm using FCM service

I have tried many solution but didn't get any dice.I am using localNotificationSchedule for android but onNotification did not fire when app is in background or kill.I think many people faceing the same issue. If any one find a way plz share.

Using GCM is fine but not working using FCM.

Any solution on that?

@Fortidude nope

same
i think there must be a rewrite because everything depends on gcm in this libary

Hello, excuse my bad english, I am using google translator.

This solution worked for me.

https://github.com/zo0r/react-native-push-notification/issues/495#issuecomment-319377505.

I share the code I used because here you can only see very little code.

example with postman to send notification.
I was researching and for FCM if you want to perform an action when you click on the application in the background, you specify click_action in the notification object

{
"to" : "Your_Mobile_Token",
"collapse_key" : "type_a",
"notification" : {
"body" : "Notificaci贸n enviada desde postman 28",
"title": "Postman",
"sound":"default",
"show_in_foreground":true,
"content_available": true,
_"click_action": "OPEN_MAIN_ACTIVITY"_
},
"data" : {
"body" : "Notificaci贸n enviada desde postman 28",
"title": "Postman"
}
}

in MainActivity.java, this code can be optimized, i use a for because bundle.getString("data") is not a results
`package com.q10soluciones.jack;

import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
import android.os.Bundle;
import com.facebook.react.ReactActivityDelegate;
import org.json.JSONObject;
import org.json.JSONException;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.ArrayList;

public class MainActivity extends ReactActivity {

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
        @Nullable
        @Override
        protected Bundle getLaunchOptions() {
            Intent mainIntent = getIntent();
            String dataValue = "hola";
            Bundle initialProps = new Bundle();
            Log.v("RNPushNotification", "main" + mainIntent);
            if (mainIntent != null) {
                Bundle bundle = mainIntent.getExtras();
                Log.v("RNPushNotification", "Data received in main: " + bundle);
                if (bundle != null) {
                    ArrayList<String> objects =new ArrayList<String>();
                    for (String key : bundle.keySet()) {
                        if(key != null){
                            objects.add(key + ": '" + bundle.get(key) + "'");
                        }
                    }

                    int count = objects.size();
                    String elements = "";
                    for(int i = 0; i < count; i++){
                        if (i < count -1){
                            elements += objects.get(i) + ",";
                        }else{
                            elements += objects.get(i);
                        }
                    }
                    String string = "{" + elements + "}";
                    Log.v("RNPushNotification", "Bundle Data" + string);
                    JSONObject data = getPushData(string);
                    Log.v("RNPushNotification", "Data received finally" + data);
                    if (data != null) {
                        try {
                            dataValue = data.toString();
                        } catch (Exception e) {
                            // no-op
                        }
                    } else {
                    }
                }
            }
            initialProps.putString("pushData", dataValue); // Read this inside your Root component in React native
            return initialProps;
        }
    };
}

private JSONObject getPushData(String dataString) {
    try {
        return new JSONObject(dataString);
    } catch (Exception e) {
        return null;
    }
}

}`

in AndroidManifest.xml specific a new intent.
image
the android.name it must be the same that notification object _"OPEN_MAIN_ACTIVITY"_

This code no active the onNotification Method.
This code return a props for use in index.android.js.
I in my code of file index.android.js import a component app.js and app.js file use console.log(this.props.pushData) and show information of the notification

pushData is the props that return in MainAplication.java
Example file App.js

class App extends Component {
componentDidMount() {
var notification = this.props && this.props.pushData ? JSON.parse(this.props.pushData) : null;
if (notification && notification.title) {
//your code
if (typeof notification.userInteraction === "undefined") notification.userInteraction = true;
}
}
}

I hope it will be a great help, as it was for me.

Greetings.

I wrestled with this issue for several hours the other day, and the thing that ultimately fixed it was changing my message payload object to only contain a data key - not notification or anything else.

More details in this comment: https://github.com/zo0r/react-native-push-notification/issues/740#issuecomment-431644518

I wrestled with this issue for several hours the other day, and the thing that ultimately fixed it was changing my message payload object to only contain a data key - not notification or anything else.

More details in this comment: #740 (comment)

mannnnn! you saved my day, I was about to commit suicide! XD

Hello, excuse my bad english, I am using google translator.

This solution worked for me.

#495 (comment).

I share the code I used because here you can only see very little code.

example with postman to send notification.
I was researching and for FCM if you want to perform an action when you click on the application in the background, you specify click_action in the notification object

{
"to" : "Your_Mobile_Token",
"collapse_key" : "type_a",
"notification" : {
"body" : "Notificaci贸n enviada desde postman 28",
"title": "Postman",
"sound":"default",
"show_in_foreground":true,
"content_available": true,
_"click_action": "OPEN_MAIN_ACTIVITY"_
},
"data" : {
"body" : "Notificaci贸n enviada desde postman 28",
"title": "Postman"
}
}

in MainActivity.java, this code can be optimized, i use a for because bundle.getString("data") is not a results
`package com.q10soluciones.jack;

import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
import android.os.Bundle;
import com.facebook.react.ReactActivityDelegate;
import org.json.JSONObject;
import org.json.JSONException;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.ArrayList;

public class MainActivity extends ReactActivity {

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
        @Nullable
        @Override
        protected Bundle getLaunchOptions() {
            Intent mainIntent = getIntent();
            String dataValue = "hola";
            Bundle initialProps = new Bundle();
            Log.v("RNPushNotification", "main" + mainIntent);
            if (mainIntent != null) {
                Bundle bundle = mainIntent.getExtras();
                Log.v("RNPushNotification", "Data received in main: " + bundle);
                if (bundle != null) {
                    ArrayList<String> objects =new ArrayList<String>();
                    for (String key : bundle.keySet()) {
                        if(key != null){
                            objects.add(key + ": '" + bundle.get(key) + "'");
                        }
                    }

                    int count = objects.size();
                    String elements = "";
                    for(int i = 0; i < count; i++){
                        if (i < count -1){
                            elements += objects.get(i) + ",";
                        }else{
                            elements += objects.get(i);
                        }
                    }
                    String string = "{" + elements + "}";
                    Log.v("RNPushNotification", "Bundle Data" + string);
                    JSONObject data = getPushData(string);
                    Log.v("RNPushNotification", "Data received finally" + data);
                    if (data != null) {
                        try {
                            dataValue = data.toString();
                        } catch (Exception e) {
                            // no-op
                        }
                    } else {
                    }
                }
            }
            initialProps.putString("pushData", dataValue); // Read this inside your Root component in React native
            return initialProps;
        }
    };
}

private JSONObject getPushData(String dataString) {
    try {
        return new JSONObject(dataString);
    } catch (Exception e) {
        return null;
    }
}

}`

in AndroidManifest.xml specific a new intent.
image
the android.name it must be the same that notification object _"OPEN_MAIN_ACTIVITY"_

This code no active the onNotification Method.
This code return a props for use in index.android.js.
I in my code of file index.android.js import a component app.js and app.js file use console.log(this.props.pushData) and show information of the notification

pushData is the props that return in MainAplication.java
Example file App.js

class App extends Component {
componentDidMount() {
var notification = this.props && this.props.pushData ? JSON.parse(this.props.pushData) : null;
if (notification && notification.title) {
//your code
if (typeof notification.userInteraction === "undefined") notification.userInteraction = true;
}
}
}

I hope it will be a great help, as it was for me.

Greetings.

HI,
with the help this code onNotification listener working into android.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmadallo1 picture ahmadallo1  路  3Comments

atteia picture atteia  路  4Comments

cidevant picture cidevant  路  3Comments

Kiran0791 picture Kiran0791  路  3Comments

DanDance picture DanDance  路  3Comments