Android : Resume last shown Activity from Notification - android

I am creating Notification in my application.
How can I resume my app from its previous position (Open previously paused activity)
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification notification = new Notification(R.drawable.txlogo, "My App", System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "My App", "simple notification", pendingIntent);
// and this
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
But in above case when i clicked on Notification it is opening "LoginActivity"
i need to resume last shown Activity.

Related

Android: Updating foreground service sometimes result in showing notification even when Foreground Service is killed

I have a foreground service with close button in the notification which killed the service. When service start & user immediately (within a second) trying to shutdown the service by clicking on close button on notification. When i am updating this foreground service notification based on Rest Api call response. In this scenario its a close call when user click on close & api response recieved at same time the update notification code is executing.The notification is still shown to user after Api response processing. Even i put a check whether foreground service is running or not but it still updating the notification making user believe that this foreground service is not stopped.
below is code fore creating forground service :
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
String title = "Creating ...";
intent.putExtra(TASK_ID, -1);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = myNotificationUtil.getBuilder(title, "Calling" + " ..");
mBuilder.setUsesChronometer(true);
mBuilder.setContentIntent(pendingIntent);
Intent stopIntent = new Intent(this, MyService.class);
stopIntent.setAction(MyIntent.STOP_FOREGROUND);
PendingIntent stopPlayIntent = PendingIntent.getService(this, 0, stopIntent, 0);
mBuilder.addAction(R.mipmap.ic_call_end_white_24dp, "Hang Up", stopPlayIntent);
startForeground(1, mBuilder.build());
below is update notification method :
private void updateForeGroundNotification(String status) {
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
String title = "Updating ...";
intent.putExtra(TASK_ID, task_id);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = myNotificationUtil.getBuilder(title, status + " ..");
mBuilder.setUsesChronometer(true);
mBuilder.setContentIntent(pendingIntent);
Intent stopIntent = new Intent(this, MyService.class);
stopIntent.setAction(MyIntent.STOP_FOREGROUND);
PendingIntent stopPlayIntent = PendingIntent.getService(this, 0, stopIntent, 0);
mBuilder.addAction(R.mipmap.ic_call_end_white_24dp, "Hang Up", stopPlayIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
I have to just update the title of notification after api call in the Notification

on click notification how to close activity before opening it, if it is running already

I have written a piece of code to start an activity on click notification. But if that activity is already open (visible) then I want to close that activity first before opening it again. Please tell how can I do that?
This is code of how I am generating notification:
Intent intent = new Intent(context, ViewReminders.class);
intent.putExtra("CALLER","GenNot");
intent.putExtra("ID",notification_id);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setTicker("Smart Locator");
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle(name);
DetailsContainer dc = new LocationDetails(context).getDetails(location);
mBuilder.setContentText(date +", "+dc.area+" "+dc.locality);
mBuilder.setContentIntent(pIntent).getNotification();
mBuilder.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(document_id, notification_id, mBuilder.build());

how to launch an activity via notification received by GCM and set text in that activitiy android?

I have developed an app in which I used GCM service to get notification, now when I received notification I want to launch an activity and in that activity I have to set a text received by GCM to a textview.My problem is that the activity which is getting launch by tapping on notification is able to set text only when the app is in foreground but not when the app is in background.
here is the code snippet I used.
#SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// Intent notificationIntent = new
// Intent().setClassName("com.ninehertz.bella",
// "com.ninehertz.bella.BellaNotificationActivity");
Intent notificationIntent = new Intent(context,
BellaNotificationActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// notification.sound = Uri.parse("android.resource://" +
// context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Try something like this.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
//put your extra message from notification and get from bundes in in onCreate in ResultActivity
resultIntent.putExtra(EXTRA_MESSAGE,message);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
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);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
you can try this code.I have no problem whether the app in foreground or background.
private static void generateNotification(Context context, String message) {
//NotificationActivity will be called when tapping notification
Intent notificationIntent = new Intent(context, NotificationActivity.class);
//this message will be carried away to NotificationActivity and you can setetxt
notificationIntent.putExtra("msg",message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder noti = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(" New message ")
.setSmallIcon(R.drawable.city)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(intent);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
noti.setAutoCancel(true);
notificationManager.notify(001,noti.build());
}

when to clear notification count of status notification in android

I have a method for creating notification. I want to clear notification number as soon as a user clicks on status notification.
public void createNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=null;
notification = new Notification(R.drawable.ic_launcher, "You have a new message", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = **count**++;
Intent intent = new Intent();
intent.setClass(TabInterfaceActivity.this, TabInterfaceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = activity;
notification.setLatestEventInfo(this, " New Message", message, notification.contentIntent);
}
If by "I want to clear notification number" you mean you want to keep the notification itself still displayed, but you want just to update/remove counter, then you should set your notification PendingIntent to point to your code that would handle this scenario for you, and when it completes it shall redirect to your target TabInterfaceActivity (or you can make it more flexible and pass target in PendingIntent's Bundle).

Android:Notification starts new activity instead of resuming old one if originally activity is not started from launcher

I have spent a week to find a solution but with no success, so I need a help.
When my application goes to background then notification appears and displays status of activity.
It works perfectly if the application is started from launcher (with action = android.intent.action.MAIN and category = android.intent.category.LAUNCHER).
But if the application is started f.e. from the gallery using action android.intent.action.SEND or android.intent.action.SEND_MULTIPLE and category android.intent.category.DEFAULT, then notification starts new activity instead of resuming existing one.
The code for notification:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
Notification notification = new Notification(icon, null, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT|Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONLY_ALERT_ONCE;
Context context = this.getApplicationContext();
CharSequence contentTitle = "XXX";
CharSequence contentText = "XXX";
Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent activityIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, activityIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
In other words, I have an application "My Application" with activity "My Activity". If "My Activity" is started on the top of gallery and creates notification, then after pressing the notification "My application" is started instead of resuming the gallery with "My activity".
Do you have any idea how to heal it?
Intent myIntent = new Intent(this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 0, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.flags = notification.flags
| Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;

Categories

Resources