Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active March 2, 2018 17:05
Show Gist options
  • Save gbzarelli/b9fdc2d66c0b1115ed3221f090666ebc to your computer and use it in GitHub Desktop.
Save gbzarelli/b9fdc2d66c0b1115ed3221f090666ebc to your computer and use it in GitHub Desktop.
Verifica se o serviço de notificação esta habilitado para o aplicativo.
// CRIAR SERVIÇO PARA INTERCEPTAR NOTIFICAÇES:
class NotificationService : android.service.notification.NotificationListenerService() {
override fun onCreate() {
super.onCreate()
}
override fun onNotificationPosted(sbn: StatusBarNotification) {
}
override fun onNotificationRemoved(sbn: StatusBarNotification) {
}
}
<!-- DECLARAR NO MANIFEST -->
<service
android:name=".services.NotificationService"
android:label="@string/notification_label"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
/**
* Is Notification Service Enabled.
* Verifies if the notification listener service is enabled.
* Got it from: https://github.com/kpbird/NotificationListenerService-Example/blob/master/NLSExample/src/main/java/com/kpbird/nlsexample/NLService.java
*
* @return True if eanbled, false otherwise.
*/
private boolean isNotificationServiceEnabled(Context context) {
String pkgName = context.getPackageName();
final String flat = Settings.Secure.getString(context.getContentResolver(),
ENABLED_NOTIFICATION_LISTENERS);
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment