androidnotificationsnotification-listener

Capturing stacked Notifications using Notification Listener


This question has been asked before but I could not find a working solution to it. I'm trying to read notifications using NotificationListener Service. Here is the code

import android.app.Notification;  
import android.content.Context;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.os.Bundle;  
import android.service.notification.NotificationListenerService;  
import android.service.notification.StatusBarNotification;  
import android.util.Log;  
import android.support.v4.content.LocalBroadcastManager;  

import java.io.ByteArrayOutputStream;  

public class NotificationService extends NotificationListenerService {  

    Context context;  

    @Override  

    public void onCreate() {  

        super.onCreate();  
        context = getApplicationContext();  

    }  
    @Override  

    public void onNotificationPosted(StatusBarNotification sbn) {  
        String pack = sbn.getPackageName();  
        String ticker ="";  
        if(sbn.getNotification().tickerText !=null) {  
            ticker = sbn.getNotification().tickerText.toString();  
        }  
        Bundle extras = sbn.getNotification().extras;  
        String title = extras.getString("android.title");  
        String text = extras.getCharSequence("android.text").toString();  
        int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);  
        Bitmap id = sbn.getNotification().largeIcon;  


        Log.i("Package",pack);  
        Log.i("Ticker",ticker);  
        Log.i("Title",title);  
        Log.i("Text",text);  

        Intent msgrcv = new Intent("Msg");  
        msgrcv.putExtra("package", pack);  
        msgrcv.putExtra("ticker", ticker);  
        msgrcv.putExtra("title", title);  
        msgrcv.putExtra("text", text);  
        if(id != null) {  
            ByteArrayOutputStream stream = new ByteArrayOutputStream();  
            id.compress(Bitmap.CompressFormat.PNG, 100, stream);  
            byte[] byteArray = stream.toByteArray();  
            msgrcv.putExtra("icon",byteArray);  
        }  
        LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);  


    }  

    @Override  

    public void onNotificationRemoved(StatusBarNotification sbn) {  
        Log.i("Msg","Notification Removed");  

    }  
} 

This works well but, when notifications get stacked, it does not capture the full notifications.

John:Hi
John:2 new messages

I have tried using the following but that too does not work.

sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();

Solution

  • The following method tries to get the extra text lines, if there are none it tries to fall back to the extra big text if it’s available.

    CharSequence[] lines = 
    extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
    if(lines != null && lines.length > 0) {
       StringBuilder sb = new StringBuilder();
       for (CharSequence msg : lines)
          if (!TextUtils.isEmpty(msg)) {
             sb.append(msg.toString());
             sb.append('\n');
          }
       return sb.toString().trim();
    }
    CharSequence chars = 
    extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
    if(!TextUtils.isEmpty(chars))
       return chars.toString();