Reputation: 1843
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>
Upvotes: 2
Views: 3903
Reputation: 1843
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!
Upvotes: 2
Reputation: 522
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)?
Upvotes: 1
Reputation: 527
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());
Upvotes: 0
Reputation: 161
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
}
}
Upvotes: 0
Reputation: 912
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);
Upvotes: 0