In my Android app I send Notification like that:
Intent intent = new Intent(ctx, MyActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(ctx, myActivityId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int icon = R.drawable.ic_launcher;
Notification n = new Notification.Builder(ctx)
.setContentTitle(messageTitle)
.setContentText(messageSubtitle)
.setSmallIcon(icon)
.setContentIntent(pIntent)
.setAutoCancel(true).getNotification();
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(myActivityId, n);
Now when I tap on notification I go to MyActivity activity. I usualy send couple notification at once. When I tap on first notification and then immediately tap on second notification I will have 2 views in my views stack. I mean when I tap back on my phone from second notification activity I'm back to first notification activity. I want to second notification activity replace first notifitaction activity that when I tap back I go to view which was displayed before I started taping on notifications. How can I do that?
Related
Currently I have this:
Intent resultIntent = new Intent(context, DigestPager.class);
and this is opening DigestPager activity (which is not launcher activity) when clicking on a single notification.
But when there are multiple notifications and they are grouped into one and collapsed, clicking on it is opening launcher activity.
Is there some simple way to open a different activity?
Intent resultIntent = new Intent(context, DigestPager.class);
resultIntent.putExtra("digestId", digest.getDigestId());
PendingIntent pendingIntent = PendingIntent.getActivity(this, digest.getDigestId(), resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(digest.getDigestId(), mBuilder.build());
Create a handler activity that opens everytime you click on a notification and based on the flags set to the notification, you can navigate to the required activity/fragment.
Hope this helps.
Below function displays a notification which pending intents. When the screen is unlock and receive notification, below method will create a notification.
When user tap on notification, it opens first ChildActivity, On backpress, it closes child activity and opens MainActivity->subActivity. on backpress closed subActivity and shows MainActivity.
Subactivity will be started based on the Intent action of MainActivity
In the unlock screen, below method gives above result and its perfect as per my requirement.
public void showNotification(Context context, String extraUri){
final Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra(MainActivity.INTENT_EXTRA_ACTIVE, 1232123);
// this will help determine to start subactivity from mainActivity
mainActivityIntent.setAction(MainActivity.MAIN_ACTIVITY_CHANGED);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addNextIntent(mainActivityIntent);
final Intent intent = new Intent(context, ChildActivity.class);
intent.putExtra(ChildActivity.EXTRA_URI, extraUri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
taskStackBuilder.addNextIntent(intent);
final PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Message Body")
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Problem
while screen is locked and tap on the notification, it ask to unlock the screen, on unlock, it starts the app. but
very first it shows SubActivity, than on back press it shows ChildActivity and on backpress of ChildActivity it shows MainActivity.
So the order of the Activities are changed.
the correct order is
MainActivity->SubActivity->ChildActivity (TopMost) (happens in unlocked screen)
Thanks in Advance.
In Android How to Restart App On Click Application "onGoing" Notification. if App is open or not.
Like As When i Click on onGoing Notification "Connected as a Media device"
You can define actions you want to happen when interacting with your Notification by adding a PendingIntent.
In the following example a PendingIntent is created to launch the (current) activity.
That intent is then added to the notification in the content section. Once this notification is shown, when you click the content section, the intent is fired and the app gets started or brought back to top.
private static final int NOTIFICATION_ID = 1;
...
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
nb.setSmallIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(getText(R.string.app_name))
.setContentText("Click to launch!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent) // Here the "onClick" Intent is added
.setOngoing(false);
nm.notify(NOTIFICATION_ID, nb.build());
In this case the notification is dismissable. If you set .setOngoing(true) you need to remove it by calling .cancel(NOTIFICATION_ID) on an instance of the NotificationManager.
See also this introduction on how to Build a Notification.
Actually I have two questions regarding notification implementation I am trying to achieve in my application:
1. Notification opening automatically : I am trying to notify on the phone during an event in my application. The notification is functioning correctly in the way that when the event is fired, the intent which I have set in the notification gets called and the screen which I need to display is shown. However the notification comes on status bar for a second and then disappears. My application which was in background automatically comes in foreground because of the notification. I don't want the application to come on foreground automatically. I just need to display the notification.
2. Displaying notification on locked screen : With the following code, I am not able to display notification on locked screen.
This is the code I have used to display the notification :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("Trip Alert");
mBuilder.setContentText(StringUtils.join("You have reached ",destination.getAddress()));
mBuilder.setNumber(++numMessages);
mBuilder.setOnlyAlertOnce(true);
mBuilder.getNotification().flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
Intent detailIntent = new Intent();
detailIntent.setClass(DestinationListActivity.this,
DestinationDetailActivity.class);
detailIntent.putExtra("Destination", destination);
startActivityForResult(detailIntent, 1);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(DestinationDetailActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(detailIntent);
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(notificationID, mBuilder.build());
The reason notification is getting displayed for a very little time is because I have written following code in the DestinationDetailsActivity onCreate :
if (getIntent().getExtras() != null) {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(100);
..
}
But if this code won't come here, how shall I dismiss the notification?
Please do let me know if I need to explain more.
MY application displays notification when it is running. But when I click the home button (the application is still running in the background) the notification disappears.
What should I do to keep it as is until the user clicks on it.
Thanks
private void showNotification() {
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
serviceIntent, 0);
Notification notification = new Notification.Builder(this)
.setTicker("Accelormeter app").setContentTitle("Acc")
.setContentText("Notification content.")
.setSmallIcon(R.drawable.noti_icon).setContentIntent(contentIntent)
.getNotification();
// Send the notification.
mNM.notify(0, notification);
}
Don't use getNotification(); instead use build(); in the Notification.Builder