Android에서 알림을 지우는 방법
프로그래밍 방식으로 알림을 지울 수 있습니까?
나는 그것을 시도했지만 NotificationManager
작동하지 않습니다. 내가 할 수있는 다른 방법이 있습니까?
알림을 취소하려면 다음 코드를 사용하십시오.
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
이 코드에는 항상 알림에 사용되는 동일한 ID가 있습니다. 취소해야하는 다른 알림이있는 경우 알림을 만드는 데 사용한 ID를 저장해야합니다.
출처 : http://developer.android.com/guide/topics/ui/notifiers/notifications.html
사용자가 알림 창에서 선택할 때 상태 표시 줄 알림을 지우려면 알림 개체에 "FLAG_AUTO_CANCEL"플래그를 추가합니다. cancel (int)을 사용하여 수동으로 지우거나 알림 ID를 전달하거나 cancelAll ()을 사용하여 모든 알림을 지울 수도 있습니다.
하지만 Donal이 맞습니다. 생성 한 알림 만 지울 수 있습니다.
아무도 이것에 대한 코드 답변을 게시하지 않았기 때문에 :
notification.flags = Notification.FLAG_AUTO_CANCEL;
.. 이미 플래그가있는 경우 다음과 같이 OR FLAG_AUTO_CANCEL 할 수 있습니다.
notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
NotificationManager에 제공된 기본 방법을 시도하십시오 .
NotificationManager.cancelAll()
모든 알림을 제거합니다. NotificationManager.cancel(notificationId)
특정 알림을 제거합니다.
API 레벨 18 (Jellybean MR2)부터는 NotificationListenerService를 통해 자신이 아닌 다른 알림을 취소 할 수 있습니다.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class MyNotificationListenerService extends NotificationListenerService {...}
...
private void clearNotificationExample(StatusBarNotification sbn) {
myNotificationListenerService.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
Notification mNotification = new Notification.Builder(this)
.setContentTitle("A message from: " + fromUser)
.setContentText(msg)
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent)
.build();
.setAutoCancel (true)
알림을 클릭하면 해당 활동을 열고 알림 표시 줄에서 알림을 제거합니다.
NotificationCompat.Builder
(의 일부 android.support.v4
)를 사용 하는 경우 객체의 메서드를 호출하기 만하면됩니다.setAutoCancel
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
어떤 사람들은 setAutoCancel()
그들에게 효과가 없다고 보고 했으므로이 방법으로 시도해보십시오.
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
참고 이 방법이 getNotification()
사용되지 않습니다 !
다음을 사용하여 포 그라운드에서 시작된 서비스에서 알림을 생성하는 경우
startForeground(NOTIFICATION_ID, notificationBuilder.build());
그런 다음 발행
notificationManager.cancel(NOTIFICATION_ID);
알림이 취소되지 않고 알림이 상태 표시 줄에 계속 나타납니다. 이 특별한 경우에는 다음을 발행해야합니다.
stopForeground( true );
서비스 내에서 다시 백그라운드 모드로 전환하고 동시에 알림을 취소합니다. 또는 알림을 취소하지 않고 백그라운드로 푸시 한 다음 알림을 취소 할 수 있습니다.
stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSound(soundUri);
builder.setAutoCancel(true);
알림 작성기를 사용하는 경우 작동합니다.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager) getApplicationContext()
.getSystemService(ns);
Nmang .cancel(getIntent().getExtras().getInt("notificationID"));
더 많은 참조를 원하시면 여기를 클릭하십시오 http://androiddhina.blogspot.in/2015/01/how-to-clear-notification-in-android.html
Actually as answered before starting with API Level 18 you can cancel Notifications posted by other apps differet than your own using NotificationListenerService but that approach will no longer work on Lollipop, here is the way to remove notifications covering also Lillipop API.
if (Build.VERSION.SDK_INT < 21) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}
All notifications (even other app notifications) can be removed via listening to 'NotificationListenerService' as mentioned in NotificationListenerService Implementation
In the service you have to call cancelAllNotifications()
.
The service has to be enabled for your application via:
‘Apps & notifications’ -> ‘Special app access’ -> ‘Notifications access’.
참고URL : https://stackoverflow.com/questions/2665634/how-to-clear-a-notification-in-android
'program tip' 카테고리의 다른 글
프로그래밍 방식으로 UIImage보기를 만드는 방법-Swift (0) | 2020.08.31 |
---|---|
순간 js는 이번 달의 첫날과 마지막 날을 얻습니다. (0) | 2020.08.31 |
Android 5.0의 AppCompat CardView에서 XML의 고도 설정 (0) | 2020.08.31 |
컨트롤러에 대한 기본 클래스를 사용하지 않고 모든 뷰에 대한 ViewBag 속성을 설정하는 방법은 무엇입니까? (0) | 2020.08.31 |
“libxml2 라이브러리에서 xmlCheckVersion 함수를 찾을 수 없습니다. (0) | 2020.08.31 |