android displaying multiple activity in saved state when notification click - android

I have a notification on my app. It works in foreground well. When i`m in a other activity and press home button after that i click notification. It opens given activity, but i need to display previous activity in a saved state.
Notification note = new Notification(R.drawable.service_state_0,
getResources().getText(R.string.serviceStarted),
System.currentTimeMillis());
Intent i = new Intent(this, StartingActivity_.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
note.setLatestEventInfo(this,
getResources().getText(R.string.app_name), getServiceStateDescription(CustomService.this), pi);
note.flags |= Notification.FLAG_NO_CLEAR;
note.flags |= Notification.FLAG_ONGOING_EVENT;
startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, note);
It always shows StartingActivity, the question is that how to call resuming activity when notification is clicked. If the activity has not already been destroyed and i can just call that instance back(resume it) and therefore not needing to load it again and won't need to add another activity to my stack. Is it possible?

I find a solution like open recently activity in Notification:
NotificationManager nm = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.flag = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Intent nIntent = new Intent();
nIntent = getPreviousIntent();
PendingIntent pi = PendingIntent.getActivity(this, 0, nIntent, 0);
notification.setLatestEventInfo(getContext(), "some Title", "some text", pi);
nm.notify(0,notification);
private Intent getPreviousIntent(Intent newIntent) {
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
String myPkgNm = getPackageName();
if (!recentTaskInfos.isEmpty()) {
ActivityManager.RecentTaskInfo recentTaskInfo;
for (int i = 0; i < recentTaskInfos.size(); i++) {
recentTaskInfo = recentTaskInfos.get(i);
if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
newIntent = recentTaskInfo.baseIntent;
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
}
return newIntent;
}
you have to add <uses-permission android:name="android.permission.GET_TASKS" />

Related

Pending Intent Not Working, Notification Does Not Open Activity

Hello I have the following function that displays notifications in my app, however I suddenly started having an issue where when I click a notification it does not take me to the activity I specified in .setContentIntent(), this is my code:
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void getNotifications() {
ArrayList<com.xxxxxx.app234929.models.Notification> notifications = mNotificationsTable.get();
int requestId = (int) System.currentTimeMillis();
Intent notificationsIntent = new Intent(getApplicationContext(), NotificationsActivity.class);
notificationsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), requestId,
notificationsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle(new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_stat_mtgh_notification)
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentTitle("xxxxxx")
.setContentText("You have "+(notifications.size() > 1 ? "new updates" :"a new update"))
.setNumber(notifications.size())
.setContentIntent(intent)
.setDefaults(Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS))
.setBigContentTitle("xxxxxx");
for (int i=0; i < notifications.size(); i++) {
inbox.addLine(notifications.get(i).getMessage());
};
Notification notification = inbox.build();
NotificationManager notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
I've googled a lot of solutions and tried them but none of them worked for me so now I do not know what I am doing wrong here.
This is the code i use and works perfectly, i have it on a service but you can use in any activity:
Intent myIntent = new Intent(MyActivity.this, ActivityIWantToOpenOnClick.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MyActivity.this, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(MyActivity.this);
ncomp.setContentTitle("Message Tittle");
ncomp.setContentText("Message Text");
ncomp.setTicker("Message Ticker");
ncomp.setSmallIcon(R.drawable.your_icon);
ncomp.setAutoCancel(true);
ncomp.setContentIntent(pendingIntent);
nManager.notify((int) System.currentTimeMillis(), ncomp.build());

notification can't go current activity

In my notification, I want to start current activity when click in pending intent. In google, many ways found and I tried many times, it didn't work. here is my code,
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
String startTime = intent.getStringExtra("Strar_time");
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis(); // notification time
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= notification.FLAG_AUTO_CANCEL;
notification.vibrate = new long[]{100, 200, 100, 500};
notification.defaults = Notification.DEFAULT_ALL;
Intent notificationIntent = new Intent(this, Goo.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP/* | Intent.FLAG_ACTIVITY_SINGLE_TOP*/);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "Your time is "+startTime, contentIntent);
notification.contentIntent=contentIntent;
nm.notify(NOTIF_ID, notification);
NOTIF_ID++;
Log.i("NotiID",NOTIF_ID+"");
}
Try this function given below,
private static void generateNotification(Context context, String message) {
int icon = R.drawable.launch_icon;
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 = null;
notificationIntent = new Intent(context, Main.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Only one working way I found so far is to make a dummy activity, start it and finish it in onRusume(). However this technique only bring application to front and do not start application when it is closed.
Then I used following workaround to bring application on front if it is alive in background otherwise start it.
Manifest:
<application>
.
.
.
<activity
android:name=".DummyActivity"
/>
.
.
</application>
In code:
public class DummyActivity{
public static boolean isAppAlive;
#Override
protected void onResume() {
// start application if it is not alive
if(!isAppAlive){
PackageManager pmi = getPackageManager();
Intent intent = pmi.getLaunchIntentForPackage(Your applications package name);
if (intent != null){
startActivity(intent);
}
}
finish();
}
In your main activity's onCreateMethod.
#Override
public void onCreate(Bundle savedInstanceState) {
DummyActivity.isAppAlive = true;
}
It is obvious that when your application is fully closed, isAppAlive will restore to its default value which is false. Thus application will be start.

Android : Resume last shown Activity from Notification

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.

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;

Android notification click, run the main intent?

Hy!
public static void addNotification(String sAppname, String sDescription) {
NotificationManager notifManager = (NotificationManager) mycontext.getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, sDescription, System.currentTimeMillis());
Intent i = new Intent (mycontext, mStart.class)
.setAction(Intent.ACTION_MAIN)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(mycontext, 0, i, 0);
note.setLatestEventInfo(mycontext, sAppname, sDescription, contentIntent);
notifManager.notify(NOTIFY_ID, note);
}
mStart is my first activity
if i click on the notification this code is "re-create" the intent, and show up a new one.
i dont want this. i d like to show up the existing main intent. how to?
The solution is:
in manifest file add the following: singleTask="true"

Categories

Resources