How to check if activity is triggered by notification

I have a problem, my MainActivity can be created in three ways: 1) a standard application to run 2) from a service 3) from notifications click. How can I check when it starts with a 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()); } 
+8
android
source share
1 answer

add

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

to code that starts the intent with notifications, and the same for other calls, startActivity just changes the value, and then inside your activity

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

for more details on this question: How to get additional data from intent on Android?

+8
source share

All Articles