Répondre

0

Il fonctionnera à partir de: Android 4.3 (JELLY_BEAN_MR2).

  1. Ajouter à votre nouvelle autorisation manifeste android.permission.BIND_NOTIFICATION_LISTENER_SERVICE.

  2. Créez une classe NotificationListenerService et ajoutez-la au manifeste.
    de Google Développeur:

doit être requise par un NotificationListenerService, pour faire en sorte que seul le système peut se lier à elle.

AndroidManifest.xml:

<service android:name=".NotificationListener" 
     android:label="@string/service_name" 
     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> 
    <intent-filter> 
     <action android:name="android.service.notification.NotificationListenerService" /> 
    </intent-filter> 
</service> 
  1. Remplacer la onNotificationPosted().

NotificationListenerService Classe:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
public class NotificationListenerService extends NotificationListenerService { 

@Override 
public void onNotificationPosted(final StatusBarNotification sbn) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
// Here you need to play with the sbn object and get the data that you want from it. 

     //sbn.getPackageName() 
     Notification notification = sbn.getNotification(); 
     Bundle extras = null; 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
      extras = notification.extras; 
     } 

     if (extras != null) { 
      extras.get("android.title"); 
     } 
    } 
} 

    @Override 
    public void onNotificationRemoved(StatusBarNotification sbn) { 
    } 

    @Override 
    @TargetApi(Build.VERSION_CODES.N) 
    public void onListenerDisconnected() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
      // Notification listener disconnected - requesting rebind 
      requestRebind(new ComponentName(this, NotificationListenerService2.class)); 
     } 
    } 
} 

Référence du développeur: https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

exemple d'application simple pour la mise en œuvre: https://github.com/kpbird/NotificationListenerService-Example/