Skip to content Skip to sidebar Skip to footer

How Opening The Targeted Activity On Click Of Notification When App Is In Background

I have sent notification through firebase console using key vale pair and handled the notification in launcher activity. below is the tried code: @Override protected void onNewInte

Solution 1:

If you need to navigate any Activity then there should be bind with Notification class(NotificationCompat.Builder) which is missing in implementation.

Below line is very important to redirect any Activity:

NotificationCompat.Builderbuilder=new **NotificationCompat.Builder(this); builder.setContentIntent(resultPendingIntent);**

Here resultPendingIntent is used for to containing the backstack for the Activity with the notifications as below:

// Gets a PendingIntent containing the entire back stack PendingIntent

resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

The complete code snippet is available here.

intid=1;

IntentresultIntent=newIntent(this, ResultActivity.class);
TaskStackBuilderstackBuilder= TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stackPendingIntentresultPendingIntent=
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManagermNotificationManager=
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());

Edit:

  • Let me explain how it will works
  • Suppose you are set response from server as below

    { "registration_ids": ["XXX", ...], "data": { "id_offer": "41" }, "notification": { "title": "This is the Title", "text": "Hello I'm a notification", "icon": "ic_push", "click_action": "ACTIVITY_XPTO" } }

And In your Manifest File

<activityandroid:name=".ActivityXPTO"android:screenOrientation="sensor"android:windowSoftInputMode="stateHidden"><intent-filter><actionandroid:name="ACTIVITY_XPTO" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter></activity>

When the app is closed or in background and the user clicks on the notification it opens my ActivityXPTO, to retrieve the id_offer I only need to do:

publicclassActivityXPTOextendsAppCompatActivity { 
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
        StringidOffer="";
        IntentstartingIntent= getIntent();
        if (startingIntent != null) {
            idOffer = startingIntent.getStringExtra("id_offer"); 
        // Retrieve the id
        }
        getOfferDetails(id_offer);
        }
    }
}

And In Second Way

  • Send key through data payload like below and get key in MainActivity via getIntent() and call specific activity or fragments.

    json1.put("title","Your Title");

    json1.put("body","body content");

    json1.put("message","Your Message");

    json1.put("screen","2"); //secondFragment is 2nd position in nav drawer

    json.put("data", json1);

Sample project on GitHub.

Solution 2:

Nothing works for me. The thing work for me is simple. Make sure you add this in the activity that you want to open directly.

<intent-filter><actionandroid:name="MainActivity" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter>

And from the push notification you must add a new payload: click_action it will looks like this -

"notification":{"title":"hello","body":"test message","click_action":"MAIN_ACTIVITY"},

Note: You can name it as you want MAIN_ACTIVITY but must be same in both place.

Post a Comment for "How Opening The Targeted Activity On Click Of Notification When App Is In Background"