[英]Keep notification icon in the status bar when clicked
我想在我的應用程序正在運行並執行某些操作時顯示一個簡單的通知。 我希望用戶單擊我的通知時打開我的應用程序。 到目前為止,該行為按我希望的那樣進行。 但是,當用戶單擊通知時,在打開應用程序且通知仍然存在的情況下,通知欄中的通知圖標(向下滑動之前為左)將消失。 編輯:我剛剛觀察到,一旦其他任何通知(WhatsApp)到達,通知圖標就會重新出現在通知欄中。 我如何防止這種行為?
通知如下所示:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
builder.setContentTitle("Running")
.setContentText("App is running.")
.setSmallIcon(R.drawable.ic_lock_outline_black_24dp)
.setOngoing(true)
.setContentIntent(intent);
notifyManager.notify(appRunningNotificationID, builder.build());
看下面的代碼:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Running")
.setContentText("App is running")
.setTicker("My app")
.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
long[] vibrate = { 500,1000 };
notification.vibrate = vibrate;
mNotificationManager.notify(appRunningNotificationID, notification);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.