Android : 알림 표시 줄에서 알림 제거
응용 프로그램을 만들었고 Android 알림 표시 줄에 알림을 추가하는 이벤트가 있습니다. 이제 이벤트의 알림 표시 줄에서 해당 알림을 제거하는 방법에 대한 샘플이 필요합니다 ??
이것은 매우 간단합니다. cancel
또는 cancelAll
NotificationManager에 전화해야합니다 . cancel 메소드의 매개 변수는 취소해야하는 알림의 ID입니다.
API 참조 : http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
이 빠른 코드를 시도 할 수 있습니다
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
cancelAll
알림 관리자를 호출 하여 알림 ID에 대해 걱정할 필요도 없습니다.
NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
편집 : 나는 downvoted 그래서 어쩌면 이것이 응용 프로그램에서 알림을 제거하도록 지정해야합니다.
다음 코드와 같이 setAutoCancel (True)을 설정하기 만하면됩니다.
Intent resultIntent = new Intent(GameLevelsActivity.this, NotificationReceiverActivityAdv.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
GameLevelsActivity.this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext()).setSmallIcon(R.drawable.icon)
.setContentTitle(adv_title)
.setContentText(adv_desc)
.setContentIntent(resultPendingIntent)
//HERE IS WHAT YOY NEED:
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(547, mBuilder.build());`
이것은 도움이 될 것입니다 :
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
이 앱에서 만든 모든 알림을 제거해야합니다
전화로 알림을 작성하면
startForeground();
서비스 내에서 전화해야 할 수도 있습니다.
stopForeground(false);
먼저 알림을 취소하십시오.
당신이 발생하는 경우 서비스에서 알림을 에서 시작 전경 사용
startForeground(NOTIFICATION_ID, notificationBuilder.build());
그런 다음 발행
notificationManager.cancel(NOTIFICATION_ID);
작동하지 않습니다. 알림 및 알림을 취소해도 여전히 상태 표시 줄에 나타납니다. 이 특별한 경우 두 가지 방법으로 문제를 해결합니다.
1> 서비스 내부에서 stopForeground (false) 사용 :
stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
2> 호출 활동으로 해당 서비스 클래스를 삭제하십시오.
Intent i = new Intent(context, Service.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
if(ServiceCallingActivity.activity != null) {
ServiceCallingActivity.activity.finish();
}
context.stopService(i);
두 번째 방법은 음악 플레이어 알림을 더 선호합니다. 왜냐하면 알림은 제거뿐만 아니라 플레이어도 제거하기 때문입니다 ... !!
NotificationManager.cancel(id)
is the correct answer. Yet you can remove in Android Oreo and later notifications by deleting the whole notification channel. This should delete all messages in the deleted channel.
Here is the example from the Android documentation:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
Please try this,
public void removeNotification(Context context, int notificationId) {
NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(notificationId);
}
Use the NotificationManager to cancel your notification. You only need to provide your notification id
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
also check this link See Developer Link
On Android API >=23 you can do somehting like this to remove a group of notifications.
for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
mNotificationManager.cancel(statusBarNotification.getId());
}
}
Just call ID:
public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
참고URL : https://stackoverflow.com/questions/3595232/android-remove-notification-from-notification-bar
'development' 카테고리의 다른 글
플로팅 액션 버튼 (패브)의 아이콘 크기 조정 (0) | 2020.06.08 |
---|---|
테이블과 tr 너비로 정렬 가능한 jquery UI (0) | 2020.06.08 |
~ x + ~ y == ~ (x + y)는 항상 거짓입니까? (0) | 2020.06.08 |
펠리칸 3.3 pelican-quickstart 오류“ValueError : unknown locale : UTF-8” (0) | 2020.06.08 |
리눅스 터미널에서 두 파일 비교 (0) | 2020.06.08 |