-1

i am making a notification that open an activity and also send extras with it when clicked but i use all the ways but it always open my launcher activity when i click on it, my code is this

public class AlarmNotificationReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    sendNotification(context);

     }


public void sendNotification(Context context) {


    int NOTIFICATION_ID = 234;
    String CHANNEL_ID = "my_channel_01";

    Intent intent = new Intent(context, TestActivity.class);
    intent.putExtra("title", "Today,s Dish");
    intent.putExtra("getDetailShowId", 50);
    intent.setAction("MyIntent");
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        //    String CHANNEL_ID = "my_channel_01";
        CharSequence name = "Title";
        String Description = "Its new";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.setDescription(Description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mChannel.setShowBadge(false);
        notificationManager.createNotificationChannel(mChannel);



    }





    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Title")
            .setContentText("Its old")
            .setContentIntent(contentIntent)
            .setAutoCancel(true)
            ;

    Intent resultIntent = new Intent(context, MenuActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);
    notificationManager.notify(NOTIFICATION_ID, builder.build());

}

i am not using Oreo so forget it, and i am getting extras like this, but this code does not work it always open my launch activity

toolbartitle = getIntent().getStringExtra("title");
    detailShowUrl = getIntent().getStringExtra("getDetailShowId");

1 Answers1

1

TL;DR

  • Create a notification channel
  • Create a notification
  • Attach a pending intent in notification in which you will add extra data
  • When notification clicked, pendingIntent will launch an activity
  • Handle what will happen inside launched activity based on pending intent extra data

Some code/resources for doing the above steps:

How to create a notification channel:

Android O - Notification Channels and NotificationCompat

How to create a pendingIntent:

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("data", json.toString());
intent.putExtra("has_extra_data", true);

PendingIntent pendingIntent = PendingIntent.getActivity(this, json.getInt("id") /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);

How to insert pendingIntent in notification and show it:

notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("title")
                        .setContentText("description")
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(id/* ID of notification */, notificationBuilder.build());

Now add this to MainActivity to handle the notification's data:

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (getIntent().getExtras().getBoolean("has_extra_data")){
        String data = getIntent().getExtras().getString("data");
        // Do what you want ...
        // You may want to start a new activity or show something in UI
    }
}

Note based on your question:

If you want to open another activity apart from MainActivity you should do it by setting custom variables in you pendingIntent extras and let MainActivity to redirect you into another Activity. Essentially, MainActivity acts as a router!

Kwnstantinos Nikoloutsos
  • 1,832
  • 4
  • 18
  • 34