简体   繁体   English

应用程序启动时关闭android通知

[英]Close android notification when application starts

I have a system notification in my application which will notify the user on receiving a certain event when the application is in background. 我的应用程序中有一个系统通知,当应用程序处于后台时,它将在收到特定事件时通知用户。 When I open the app by clicking the notification, app opens fine and dismisses the notification. 当我通过单击通知打开应用程序时,应用程序会正常打开并关闭通知。 However, when I open the app using the app icon(either from notification bar or home screen) notification is not dismissed. 但是,当我使用应用程序图标(从通知栏或主屏幕)打开应用程序时,通知不会关闭。

Is there a way to dismiss notification (if it is visible in notification bar) on starting the app? 启动应用程序时,是否有办法消除通知(如果在通知栏中可见)?

Please find my notification code below: 请在下面找到我的通知代码:

final int notificationId = 9999;

final NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Notification title")
    .setContentText("Notification text")
    .setAutoCancel(true)
    .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0));
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());

Just do this: 只要这样做:

mNotificationManager.cancel(notificationId);

or cancel all your app's notifications: 或取消您所有应用的通知:

mNotificationManager.cancelAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM