11

I have problem my MainActivity can be created by 3 ways: 1) standart launch App 2) from Service 3) from notification click. How I can check when it starts from notification click?

Notification code:

private void createNotification()
{
    Log.d("service createNotification",MainActivity.TAG);
    Context context = getApplicationContext();
    Intent notificationIntent = new Intent(this,MainActivity.class);
    intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(this.getString(R.string.notification_title))
            .setContentText(this.getString(R.string.notification_text))             
            .setContentIntent(pendingIntent)             
            .setSmallIcon(R.drawable.ic_launcher);

    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification());
}
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Abbath
  • 1,002
  • 13
  • 30

1 Answers1

8

add

intent.putExtra("started_from","notification");

to the code that starts the intent from the notifications, and the same thing to the other startActivity calls just change the value, then inside your activity

String startedFrom = getIntent().getStringExtra("started_from");

for more refer to this question: How do I get extra data from intent on Android?

Community
  • 1
  • 1
Ali Alnoaimi
  • 2,278
  • 2
  • 21
  • 31