我有問題,關於如何關閉應用程序,當用戶點擊通知從托盤和打開活動。 整個場景如下: 例如我的應用程序名稱是testdemo
。如何關閉當前打開的應用程序,當點擊推送通知
假設testdemo
應用程序當前打開,用戶在應用程序中做一些工作。當時推送通知到達與testdemo
應用程序有關。
在這種情況下,用戶已經打開testdemo
應用程序,但當點擊通知時,它再次打開testdemo
應用程序。但是我想關閉當前的應用程序而不是打開的應用程序。由於當新應用程序關閉的時候也是testdemo
應用程序,但是最後一個屏幕已經被用戶點擊通知之前打開。
我發送通知如下:
private void sendNotification(String title,
String message,
String receiverUid) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("uid", receiverUid);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ++NOTIFICATION_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_alert)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notificationBuilder.build());
}
Thnks的答案。你能給出例子或引用的鏈接,以獲得確切的想法。 – ashish
@ashish,好的。 DONE – Vyacheslav