Reputation: 638
I want to resume my app, exactly at where I left from notification icon in bar. Then I used this code:
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_speed)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.txt_welcome))
.setAutoCancel(true).setDefaults(Notification.DEFAULT_LIGHTS).setWhen(System.currentTimeMillis())
.setOngoing(true);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
It works well, the issue is when I press the icon in notification bar, it disappears, I need it to stay there.
Of course, if I delete these three lines:
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in manifest.xml
<activity android:name=".MainActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>
the icon won't disappear after clicking on it, but I always redirect to MainActivity, not the same place I left (when app got into pause).
any idea?
Upvotes: 0
Views: 144
Reputation: 34200
please refer below example and please let me know if it is not working
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager = null;
private NotificationCompat.Builder mNotificationBuilder;
if (mNotificationManager == null) {
mNotificationManager = (android.app.NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
if (mNotificationBuilder == null) {
mNotificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.app_icon_96x96)
.setContentText(getContentString(false, 0))
.setAutoCancel(false)
.setPriority(Notification.PRIORITY_MAX)
.setOngoing(true)
.setCategory(NOTIFICATION_SERVICE);
}
Upvotes: 0
Reputation: 4284
Try setting :
setAutoCancel(false)
Reference : https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)
Hope it helps !
Edit :
As per discussion, need to set in manifest :
android:launchMode="singleTop"
This will deliver new intent to "onNewIntent" function which can be used to do further actions in case your activity was already running.
Upvotes: 1
Reputation: 57
Try this code..!
void notifyme(String string){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(ns);
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = string + " Program Running..."; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = *********; // expanded message title
CharSequence contentText = string + " Program Running...";//expanded msg text
Intent notificationIntent = new Intent(this, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations
// above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
After add this
final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Upvotes: 0
Reputation: 302
Notification should be onGoing
mBuilder.setOngoing(true); //this will make ongoing notification
Upvotes: 0