@SuppressLint("DefaultLocale") public static ArrayList<String> getNotificationText(Notification notification) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Bundle extras = notification.extras; ArrayList<String> notificationData = new ArrayList<String>(); if (extras.get("android.title") != null) notificationData.add(extras.get("android.title").toString()); if (extras.get("android.text") != null) notificationData.add(extras.get("android.text").toString()); if (extras.get("android.subText") != null) notificationData.add(extras.get("android.subText").toString()); return notificationData; } else { RemoteViews views = notification.contentView; Class<?> secretClass = views.getClass(); try { ArrayList<String> notificationData = new ArrayList<String>(); Field outerFields[] = secretClass.getDeclaredFields(); for (int i = 0; i < outerFields.length; i++) { if (!outerFields[i].getName().equals("mActions")) continue; outerFields[i].setAccessible(true); @SuppressWarnings("unchecked") ArrayList<Object> actions = (ArrayList<Object>) outerFields[i].get(views); for (Object action : actions) { Field innerFields[] = action.getClass().getDeclaredFields(); Object value = null; for (Field field : innerFields) { field.setAccessible(true); // Value field could possibly contain text if (field.getName().equals("value")) { value = field.get(action); } } // Check if value is a String if (value != null && value.getClass().getName().toUpperCase().contains("STRING")) { notificationData.add(value.toString()); } } return notificationData; } } catch (Exception e) { e.printStackTrace(); } } return null; }
public List<String> getText(Notification notification) { if (null == notification) return null; RemoteViews views = notification.contentView; if (views == null) return null; // Use reflection to examine the m_actions member of the given // RemoteViews object. // It's not pretty, but it works. List<String> text = new ArrayList<String>(); try { Field field = views.getClass().getDeclaredField("mActions"); field.setAccessible(true); @SuppressWarnings("unchecked") ArrayList<Parcelable> actions = (ArrayList<Parcelable>) field.get(views); // Find the setText() and setTime() reflection actions for (Parcelable p : actions) { Parcel parcel = Parcel.obtain(); p.writeToParcel(parcel, 0); parcel.setDataPosition(0); // The tag tells which type of action it is (2 is // ReflectionAction, from the source) int tag = parcel.readInt(); if (tag != 2) continue; // View ID parcel.readInt(); String methodName = parcel.readString(); if (null == methodName) { continue; } else if (methodName.equals("setText")) { // Parameter type (10 = Character Sequence) parcel.readInt(); // Store the actual string String t = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel).toString().trim(); text.add(t); } parcel.recycle(); } } catch (Exception e) { log("getText" + e.getMessage()); } return text; }