How to read all the coming notifications in android

前端 未结 3 1368
面向向阳花
面向向阳花 2020-12-02 08:00

How to read all the coming notifications in android. Is it possible to use the broadcast receiver to listen the incoming notifications and the ability to read the notificati

相关标签:
3条回答
  • 2020-12-02 08:30

    First you must declare your intent to receive notifications in your manifest, so that you can get the android.permission.BIND_NOTIFICATION_LISTENER_SERVICE permission.

    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>
    

    Then create a NotificationListenerService class and override the onNotificationPosted function.

    For more information, read the developer reference here: https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

    Also look at this simple example application for implementation guidance: https://github.com/kpbird/NotificationListenerService-Example/

    0 讨论(0)
  • 2020-12-02 08:30

    You need to do like this in onNotificationPosted in order to get all messages

    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)){
            Parcelable b[] = (Parcelable[]) extras.get(Notification.EXTRA_MESSAGES);
    
            if(b != null){
    
                for (Parcelable tmp : b){
    
                    Bundle msgBundle = (Bundle) tmp;
                    content = content + msgBundle.getString("text") + "\n";
    
                /*Set<String> io = msgBundle.keySet(); // To get the keys available for this bundle*/
    
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-02 08:36

    Using NotificationListenerService we can easily read the notification of all application. Check complete demo code here

    0 讨论(0)
提交回复
热议问题