Reputation: 1804
I have a foreground service, and I'd like to make the Notification open the MainActivity when the user clicks on the Notification.
The method for creating the Notification in the custom Service class is:
public void setForeground(final int notificationID, final int notificationIconID,
final String notificationTickerText,
final String notificationTitle, final String notificationText){
Notification notification = new Notification(notificationIconID, notificationTickerText,
System.currentTimeMillis());
notification.setLatestEventInfo(this, notificationTitle,
notificationText, PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()), 0));
startForeground(notificationID, notification);
}
How do I modify it to include the open (if the MainActivity is currently open) or start MainActivity on Notification click functionality?
Upvotes: 0
Views: 653
Reputation: 7901
If you want to open any Activity
by clicking notification
, then you need to implement TaskStackBuilder
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.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);
Upvotes: 2