Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

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

Closed
perfectdim opened this issue Sep 13, 2018 · 13 comments
Closed

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

perfectdim opened this issue Sep 13, 2018 · 13 comments

Comments

@perfectdim
Copy link

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
Copy link

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

@perfectdim
Copy link
Author

@bhushansethi still no

@muhsin-k
Copy link

I am also facing this issue.

@AlekseyKutsko
Copy link

AlekseyKutsko commented Sep 24, 2018

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

@rouaha
Copy link

rouaha commented Oct 1, 2018

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
Copy link

chengsam commented Oct 4, 2018

Using GCM is fine but not working using FCM.

@Fortidude
Copy link

Any solution on that?

@perfectdim
Copy link
Author

@Fortidude nope

@guitar9
Copy link

guitar9 commented Oct 14, 2018

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

@jperezr13
Copy link

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.

@mrtomhoward
Copy link

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)

@sebinator
Copy link

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
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests