2

I want to open an Activity when I click on the notification from the Status bar. I have seen this answered on StackOverflow but none of these answers work for me. This is my code:

 notificationBuilder = new NotificationCompat.Builder(this); notificationBuilder.setProgress(100, 0, false); notificationBuilder.setAutoCancel(true); notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload); notificationBuilder.setContentTitle(getString(R.string.notification_upload_title)); //when this notification is clicked and the upload is running, open the upload fragment Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK); // set intent so it does not start a new activity PendingIntent intent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); notificationBuilder.setContentIntent(intent); this.startForeground(NOTIFICATION_ID_UPLOADING_PROGRESS, notificationBuilder.build()); 

Manifest.xml

<activity android:name="com.android.app.MainActivity_" android:label="@string/app_short_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
9
  • stackoverflow.com/a/13716784/776244 Commented May 30, 2016 at 10:25
  • Why are you using this.startForeground instead of NotificationManager.notify? Commented May 30, 2016 at 10:27
  • What happens when you click on the notification? Commented May 30, 2016 at 10:28
  • @Malith Lakshan, the status bar is closed and nothing more happens. Commented May 30, 2016 at 10:29
  • 1
    Check if the context provided to the Pending intent is correct or not,try to use ClassName.this, if it is from a service or an activity Commented May 30, 2016 at 10:33

5 Answers 5

2

Finally I have realized that I have to write this:

Intent notificationIntent = new Intent(this, MainActivity_.class); 

instead of

Intent notificationIntent = new Intent(this, MainActivity.class); 

Thanks a lot for all your answers!

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think that you need all this flags and configurations just to open an activity from the PendingIntent. You certainly don't need

notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent.FLAG_ONE_SHOT 

Remove them and try again.

Furthermore: is the package name of the MainActivity as intended (com.android.app.MainActivity)?

Comments

0

try below solution hope it works for you

Intent intent = new Intent(this,YourActivity....class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent blogIntent = PendingIntent.getActivity(this, INT CONSTANTS, intent, PendingIntent.FLAG_ONE_SHOT); 

From Notification

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); . . . notificationBuilder.setContentIntent(blogIntent); Notification notification = notificationBuilder.build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(Constants.NOTIFICATION_NEW_BLOG, notification); 

Comments

0

You have to add custom receiver in manifest

 <receiver android:name=".IntentReceiver" android:exported="false"> <intent-filter> <!-- add notification action here --> </intent-filter> </receiver> 

In Intent receiver class override on recieve method

public class IntentReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //call your activity here } } 

1 Comment

I have already tried and it didnt work. This onReceived method is not called
0
Use this code /* Invoking the default notification service */ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setContentTitle("Test"); mBuilder.setContentText(text2); mBuilder.setSmallIcon(R.drawable.icon); /* Creates an explicit intent for an Activity in your app */ Intent resultIntent = new Intent(this, NotificationListActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(NotificationListActivity.class); /* Adds the Intent that starts the Activity to the top of the stack */ stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); /* notificationID allows you to update the notification later on. */ mNotificationManager.notify(9999, mBuilder.build()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.