11

I am making an Android app. I have problem, that is, My MainActivity can be created by 3 ways:

  1. standard app launch
  2. from Service
  3. and 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());
}

2 Answers 2

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?

3
  • 3
    unfochnately this method does'nt work, It always get notification in all 3 cases.
    – Abbath
    Commented Sep 1, 2013 at 18:36
  • you need to change "notification" in putExtra to something else, to "service" for example. Commented Sep 1, 2013 at 19:10
  • doesn't work at all, look @stackoverflow.com/questions/14122085/… if any1 interested Commented Dec 4, 2015 at 11:41
0

To identify which service or broadcast receiver acts as a notification trampoline in your app, look at the output of the following terminal command:

adb shell dumpsys activity service \com.android.systemui/.dump.SystemUIAuxiliaryDumpService

output includes the text "NotifInteractionLog". This section contains the information that's necessary to identify the component that starts as the result of a notification tap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.