2

I am trying to read the Actions and RemoteInputs attached to each StatusBarNotification in a NotificationListenerService. However, I want to use the compatibility library for support, so I am trying to get everything as NotificationCompat.Action and android.support.v4.app.RemoteInput.

for (StatusBarNotification sbn : NLService.this.getActiveNotifications()) {
    Notification notif = sbn.getNotification();
    NotificationCompat notifCompat = //?????????????
}

getNotification only returns a Notification instead of a NotificationCompat, and I have been very frustrated trying to convert one into the other. I am sure that there is a very simple way to do it, but when I looked at the builder classes and extender classes, I could not find anything.

dennisj
  • 83
  • 5
  • 1
    Use the static methods, e.g. `NotificationCompat.getSortKey(Notification)`, for accessing new APIs on `Notification`. – alanv Mar 26 '15 at 00:08
  • Wow, I must be blind. I sorely misunderstood how NotificationCompat and Notification interacted. That makes a lot of sense, thank you! – dennisj Mar 26 '15 at 00:11

1 Answers1

1

Many of the appcompat-v4 classes provide static methods for accessing methods added after API 4.

Many of these will no-op or return default values when called on API levels where they did not exist yet. Some will attempt to emulate the new behavior. Often they are documented as such.

For example,

// Using latest APIs
String sortKey = myNotification.getSortKey();

// Using compat APIs
String sortKey = NotificationCompat.getSortKey(myNotification);

As a side note, the implementation details for these classes are worth looking into just in case you need to write your own. They avoid reflection, which is costly, by statically loading the correct implementation classes for the current API level.

alanv
  • 23,966
  • 4
  • 93
  • 80