4

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

REG1
  • 486
  • 4
  • 15
  • The documentation does not show a key for the data you are looking for. So either it does not exist or it is not documented. You could iterate over the keys and output the values to the log to see if a key stands out. – Larry McKenzie Aug 13 '14 at 18:25
  • For what purpose you are looking to do this ? – Ajay S Aug 13 '14 at 18:38
  • @LarryMcKenzie I forgot to mention that I went through several of the keys that I thought could make sense to represent the 'body of the email', but all of them returned null. It seems not to exist...notice the key EXTRA_PICTURE: this is a bitmap to be shown in Notification.BigPictureStyle expanded notifications, supplied to bigPicture(android.graphics.Bitmap) -> I want text (not a picture) supplied by bigText(), however it does not exist. Is there any quick workaround other than java reflections? Thanks for the quick reply :) – REG1 Aug 13 '14 at 18:39
  • Instead of guessing possible keys iterate over an extras bundle and see if one exists. If it does not exist then it is not likely that you will be able to get that data. – Larry McKenzie Aug 13 '14 at 18:44
  • @LarryMcKenzie that didn't work either, I added some more information, incase you can help any more – REG1 Aug 19 '14 at 17:22
  • Try using EXTRA_TEXT to get the bigText, I looked at the source and it appears that the BigTextStyle overrides the EXTRA_TEXT in the extras bundle. – Larry McKenzie Aug 19 '14 at 17:38
  • @LarryMcKenzie I am already using EXTRA_TEXT, however that only gives me the text if its a text message notification for example but also many other notifications that only have a simple layout. The Gmail notification seems to work differently (e.g. it contains a reply button and so on). I would also think that EXTRA_TEXT must cover it but from my experience it doesn't. Thanks for the quick reply, really appreciate it :) – REG1 Aug 19 '14 at 17:45
  • The reply button isn't really different, it is just an added action, but it is possible that they may not be using the BigTextStyle Notification. In which case you will have a very hard time getting that text. If they were using the BigTextStyle you would get the text with EXTRA_TEXT. – Larry McKenzie Aug 19 '14 at 17:51
  • @LarryMcKenzie Ok, I guess I will go over everything again see whether I forgot something, thanks again for your great help :) – REG1 Aug 19 '14 at 17:59
  • @LarryMcKenzie Funny enough I had overlooked an error message I got, because it was orange and not red (in the LogCat). I added this information above under 'Update', if you wouldn't mind checking it out – REG1 Aug 19 '14 at 18:37

2 Answers2

11

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();
}
Larry McKenzie
  • 3,253
  • 25
  • 22
  • Wow, that was great, thanks a ton for your help, I will certainly come back and up vote your answer once I have enough reputation. Really nice :) – REG1 Aug 19 '14 at 19:08
1

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");
}
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • any thought on how to figure out if it generates Big Style Notifications so you know that you have to use android.bigText key instead of Notification.EXTRA_BIG_TEXT? – Boris Gafurov Oct 13 '17 at 18:33