Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable way of retrieving StatusbarNotification details (title, notification text)

I'd like to get as much information out of a StatusBarNotification-object as possible. Right now, the only "reliable" information that can be accessed is the tickerText-property. I'm using the following code to get the notification's title and text via RemoteViews, but a lot of the time, the title and/or text will simply be null :-(:

    //Get the title and text
    String mTitle = "";
    String mText = "";
    try {
        RemoteViews remoteView = sbn.getNotification().contentView;
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup localView = (ViewGroup) inflater.inflate(remoteView.getLayoutId(), null);
        remoteView.reapply(getApplicationContext(), localView);
        TextView tvTitle = (TextView) localView.findViewById(android.R.id.title);
        TextView tvText = (TextView) localView.findViewById(16908358);
        mTitle = (String)tvTitle.getText();
        mText = (String)tvText.getText();
    } catch (Exception e){
        Log.e(TAG, "Error getting notification title/text: " + e);
    }

Is there any alternative (more reliable) way? I could "hand-code" the resource IDs for "popular" notifications like Gmail, SMS, etc., but this may break at any time when those apps are updated. Thanks!

like image 589
Nick Avatar asked Aug 08 '13 09:08

Nick


1 Answers

With Android 4.4(KitKat), API Level 19 you can use Notification.extras attibute to get Notification title,text,....

http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html

like image 73
nguoitotkhomaisao Avatar answered Sep 28 '22 21:09

nguoitotkhomaisao