zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.77k stars 2.05k forks source link

onNotification do not fired when click on received push. Android #868

Closed perfectdim closed 4 years ago

perfectdim commented 6 years ago

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?

bhushansethi commented 6 years ago

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

perfectdim commented 6 years ago

@bhushansethi still no

muhsin-k commented 6 years ago

I am also facing this issue.

AlekseyKutsko commented 6 years ago

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

rouaha commented 6 years ago

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.

chengsam commented 6 years ago

Using GCM is fine but not working using FCM.

Fortidude commented 6 years ago

Any solution on that?

perfectdim commented 6 years ago

@Fortidude nope

guitar9 commented 6 years ago

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

jperezr13 commented 6 years ago

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, "contentavailable": true, **"click_action": "OPEN_MAINACTIVITY"** }, "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_MAINACTIVITY"

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.

mrtomhoward commented 6 years ago

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

sebinator commented 5 years ago

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

parassinghh commented 4 years ago

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, "contentavailable": true, **"click_action": "OPEN_MAINACTIVITY"** }, "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_MAINACTIVITY"

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.