0

I was create code by volley and service to see every time if user has a new comment in the table in db. Now the code is work fine but I have one problem. I make user can click on Notification to move to another page in my app. I need put or pass data with user to other page but I can't do it.

This my code:

public void run_loop(){

    String url =  "https://xxxxxxxxxxxxxxx/Notification.php?Id="+Id;
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("data");
                        JSONObject hit = jsonArray.getJSONObject(0);
                        String id = hit.getString("id");
                        String ma_id= hit.getString("ma_id");
                       Intent activityIntent = new Intent(MainActivity.this, MainActivityFargmainMarket.class);
                        activityIntent.putExtra("key", ma_id);
                        activityIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, activityIntent, 0);
                        int lastThread = Integer.parseInt(String.valueOf(id));
                        if (app.getTotal_threadss() <lastThread) {
                            app.setTotal_threadss(lastThread);
                            Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_1_ID)
                                    .setSmallIcon(android.R.drawable.ic_popup_reminder)
                                    .setContentTitle("title")
                                    .setContentText("message")
                                    .setAutoCancel(true)
                                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                                    .setContentIntent(contentIntent)
                                    .build();
                            notificationManager.notify(1, notification);

                        }
                    } catch(JSONException e){
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });


    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(request);

As you can see in my code I try hat by this code but that don't work

Intent activityIntent = new Intent(MainActivity.this, MainActivityFargmainMarket.class);
activityIntent.putExtra("key", markte_id);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, activityIntent, 0);

I get error:

2020-07-27 21:16:40.216 9980-9980/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 9980
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.Fragmantmarket.MainActivityFargmainMarket}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
        at com.example.myapplication.Fragmantmarket.MainActivityFargmainMarket.onCreate(MainActivityFargmainMarket.java:92)
        at android.app.Activity.performCreate(Activity.java:6942)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
        at android.app.ActivityThread.-wrap14(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6682) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

Receive the data as follows:

Intent intents = getIntent();
String extraBody = intents.getStringExtra("key");
Log.i(TAG, "ddddfg"+extraBody.toString());

Also I tried many articles here as:

how-to-send-parameters-from-a-notification-click-to-an-activity

how-to-set-click-listener-for-notification

It is similar to my code and is not working.

3 Answers3

0

You should use same key to pass and retrieve data, as you are passing data with "key" you should retrieve with "key" not "

activityIntent.putExtra("key", markte_id);

to retrieve this

String extraBody = intents.getStringExtra("key");
ahmed nader
  • 578
  • 3
  • 9
  • I used the same key but when I wrote the question here I forgot to change it not that is the cause of the error The question has been updated. –  Jul 27 '20 at 17:13
0

The problem is null object reference, which means you have a null object and you are trying to get data from.

Null object could be response

public void onResponse(JSONObject response) {

or jsonArray

JSONArray jsonArray = response.getJSONArray("data");

or hit

JSONObject hit = jsonArray.getJSONObject(0);

In the error log it says line 92, I can't see where that is, but you can.

As an advice, always check if the response is not null, then do your work, else throw exception or return an error message or so.

0

The problem is with the way you are getting the intent in the MainActivityFargmainMarket class.

Here's what you need to do:

1 - Set activityIntent flag to FLAG_ACTIVITY_SINGLE_TOP, which you already made.

2 - Set contentIntent flag to PendingIntent.FLAG_UPDATE_CURRENT, if not it will reuse the same extras for every notification.

3 - Override the onNewIntent method in the MainActivityFargmainMarket class to get the activityIntent you sent

Private Intent intent;
@Override
protected void onNewIntent(Intent _intent){
    super.onNewIntent(_intent);
    if(_intent == null)
        Log.i(TAG, "_intent is null");
    else {
        intent = _intent;
        getData();
}
private void getData(){
    String extraBody = intent.getStringExtra("key");
    Log.i(TAG, "ddddfg"+extraBody.toString());
}

do not try to get any data from intent before checking if null