Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from expanded notification in status bar?

Is it possible to get the text within an expanded notification in the statusbar? This means text set my Notification.BitTextStyle.bigText(), such as in an email notification from Gmail.

I would like to get it using Notification.extras but there seems to be no constant, such as Notification.EXTRA_BIG_TEXT for example, that allows this to work.

Is there another way of getting this text? Furthermore when I use

String bigTitle = mExtras.getString(Notification.EXTRA_TITLE_BIG;

it returns null, any idea why? I use this to get the subject of the email (Testing the new Gmail notification shortcuts -> see link below).

Below is an example of an expanded Gmail notification: (I want to get the following text ->

You now get shortcut buttons for Reply and Archive within...

Picture of Gmail Notification (Can't post pictures with less than 10 reputation, sorry)

As an alternative I have also tried the following:

RemoteViews rv = mNotification.contentView;
LayoutInflater inflater =   (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup localView = (ViewGroup) inflater.inflate(rv.getLayoutId(), null);
rv.reapply(getApplicationContext(), localView);
everything = new StringBuilder(150);

      for(int i=0; i<((ViewGroup)localView).getChildCount(); ++i) {
            View nextChild = ((ViewGroup)localView).getChildAt(i);
            try{
                TextView tv = (TextView) localView.findViewById(nextChild.getId());
                everything.append(tv.getText().toString());
            }catch(Exception e){
                continue;
            }
      }
Log.i("abc", everything.toString());

But that didn't work for me either, never seemed to get any Text from any of the views.

Update:

When I use the following code to get the text from a notification...:

body = mExtras.getString(Notification.EXTRA_TEXT);

...all works well as long as it is not a Gmail/Email notification:

As soon as I get a Gmail notification I get the following error message:

08-19 20:19:37.379: W/Bundle(6646): Key android.text expected String but value was a android.text.SpannableString.  The default value <null> was returned.
08-19 20:19:37.379: W/Bundle(6646): Attempt to cast generated internal exception:
08-19 20:19:37.379: W/Bundle(6646): java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
08-19 20:19:37.379: W/Bundle(6646):     at android.os.Bundle.getString(Bundle.java:1121)
08-19 20:19:37.379: W/Bundle(6646):     at com.myprojectabc.storage.Core$mainmethod$1.run(Core.java:394)
08-19 20:19:37.379: W/Bundle(6646):     at android.os.Handler.handleCallback(Handler.java:733)
08-19 20:19:37.379: W/Bundle(6646):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-19 20:19:37.379: W/Bundle(6646):     at android.os.Looper.loop(Looper.java:136)
08-19 20:19:37.379: W/Bundle(6646):     at android.app.ActivityThread.main(ActivityThread.java:5001)
08-19 20:19:37.379: W/Bundle(6646):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 20:19:37.379: W/Bundle(6646):     at java.lang.reflect.Method.invoke(Method.java:515)
08-19 20:19:37.379: W/Bundle(6646):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-19 20:19:37.379: W/Bundle(6646):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-19 20:19:37.379: W/Bundle(6646):     at dalvik.system.NativeStart.main(Native Method)

If I then try to solve that problem by changing it to this...

body = mExtras.getString(Notification.EXTRA_TEXT).toString();

...I get a NullPointerException

Would really appreciate it if someone could help me out here

Thanks, REG1

like image 279
REG1 Avatar asked Dec 15 '22 20:12

REG1


2 Answers

Okay so with this new information I would try something like this:

SpannableString bigText = (SpannableString) mExtras.get(Notification.EXTRA_TEXT);
if(bigText != null){
    body = bigText.toString();
}

Edit: After reviewing the source I would try this:

CharSequence bigText = (CharSequence) mExtras.getCharSequence(Notification.EXTRA_TEXT);
if(bigText != null){
    body = bigText.toString();
}
like image 86
Larry McKenzie Avatar answered Jan 04 '23 18:01

Larry McKenzie


When any notification generates Big Style Notification, expanded text can be fetched with android.bigText key :

if(mExtras.getCharSequence("android.bigText")) {
    String body = mExtras.getCharSequence("android.bigText");
}
like image 43
Kushal Avatar answered Jan 04 '23 19:01

Kushal