반응형
Android 4.1 : 애플리케이션에 대한 알림이 비활성화되었는지 확인하는 방법은 무엇입니까?
Android 4.1은 사용자에게 특정 애플리케이션에 대한 알림을 비활성화하는 확인란을 제공합니다.
그러나 개발자로서 알림 호출이 효과적인지 여부를 알 수있는 방법이 없습니다.
현재 응용 프로그램에 대해 알림이 비활성화되어 있는지 확인해야하지만 API에서 해당 설정을 찾을 수 없습니다.
코드에서이 설정을 확인하는 방법이 있습니까?
당신은 100 % 할 수 없습니다.
이 Google I / O 2012 동영상 에서 요청한 내용이며 새 알림에 대한 프로젝트 책임자는 할 수 없다고 선언합니다.
편집하다
2016 업데이트 : 이제이 Google I / O 2016 동영상 에서 언급 한대로 확인할 수 있습니다 .
사용 NotificationManagerCompat.areNotificationsEnabled()
알림 API 19+ 차단하는 경우 지원 라이브러리는 확인합니다. API 19 이하 버전은 true를 반환합니다 (알림 활성화 됨).
실제로 이것은 매우 쉽습니다.
/**
* Created by desgraci on 5/7/15.
*/
public class NotificationsUtils {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
public static boolean isNotificationEnabled(Context context) {
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (int)opPostNotificationValue.get(Integer.class);
return ((int)checkOpNoThrowMethod.invoke(mAppOps,value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
}
@blundell의 답변은 정확하지만 최신 버전에는 사소한 변경 사항이 있습니다.
NotificationManagerCompat.from(context).areNotificationsEnabled()
Xamarin을 사용 중이고이 답변이 필요한 경우 다음 코드를 사용할 수 있습니다.
//return true if this option is not supported.
public class NotificationsUtils
{
private const String CHECK_OP_NO_THROW = "checkOpNoThrow";
private const String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
public static bool IsNotificationEnabled(global::Android.Content.Context context) {
AppOpsManager mAppOps = (AppOpsManager) context.GetSystemService(global::Android.Content.Context.AppOpsService);
ApplicationInfo appInfo = context.ApplicationInfo;
String pkg = context.ApplicationContext.PackageName;
int uid = appInfo.Uid;
try {
var appOpsClass = Java.Lang.Class.ForName("android.app.AppOpsManager");
var checkOpNoThrowMethod = appOpsClass.GetMethod(CHECK_OP_NO_THROW,Java.Lang.Integer.Type,Java.Lang.Integer.Type,new Java.Lang.String().Class);//need to add String.Type
var opPostNotificationValue = appOpsClass.GetDeclaredField (OP_POST_NOTIFICATION);
var value = (int)opPostNotificationValue.GetInt(Java.Lang.Integer.Type);
var mode = (int)checkOpNoThrowMethod.Invoke(mAppOps,value, uid, pkg);
return (mode == (int)AppOpsManagerMode.Allowed);
} catch (Exception)
{
System.Diagnostics.Debug.WriteLine ("Notification services is off or not supported");
}
return true;
}
}
알림 상태를 쿼리 할 방법이없는 것 같습니다.
나는 이것을 추천한다 :
- 알림으로 애플리케이션을 디자인하세요.
- 사용자가 애플리케이션 설정에서 알림을 비활성화 할 수 있도록합니다.
- 알림이 클릭되었는지 확인하십시오. 사용자가 알림을 클릭하면이를 기본 설정에 저장합니다.
- 앱에서 알림 설정이 켜져 있고 사용자가 Android 4.1 이상 (API 16) 인 경우 사용자가 며칠 / 주 동안 알림을 클릭하지 않으면 사용자가 알림을 비활성화했다고 가정합니다.
100 % 정확하지 않습니다. 그러나 이것은 의견을 제공합니다.
예를 들어 사용자가 10 ~ 15 일 동안 앱 알림을 클릭하지 않으면 비활성화했을 수 있습니다.
반응형
'programing tip' 카테고리의 다른 글
netbeans Java 프로젝트의 .gitignore 파일에는 무엇이 있어야합니까? (0) | 2020.09.10 |
---|---|
Python에서 사전을 복사하는 빠른 방법 (0) | 2020.09.10 |
ensureSuccessStatusCode 사용 및 발생하는 HttpRequestException 처리 (0) | 2020.09.09 |
열거 형이 Swift의 프로토콜을 준수하도록 만드는 방법은 무엇입니까? (0) | 2020.09.09 |
LINQ Distinct 연산자, 대소 문자 무시? (0) | 2020.09.09 |