mercredi 22 juin 2016

Get Message list from notification

Starting from Android N, it's possible to retrieve a list of Message objects. The extra key is EXTRA_MESSAGES. However you need to convert Parcelable[] array. There is a package static private method but the problem is that the invoke method doesn't understand the array that should be wrapped into another one. This is my code:

private List<Notification.MessagingStyle.Message> extractMessages(Notification notification) {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
            return null;
        }
        List<Notification.MessagingStyle.Message> list;
        Parcelable[] parcelables = notification.extras.getParcelableArray(Notification.EXTRA_MESSAGES);
        Method m;
        try {
            m = Notification.MessagingStyle.Message.class.getDeclaredMethod("getMessagesFromBundleArray",
                    Parcelable[].class);
            m.setAccessible(true);
            list = (List<Notification.MessagingStyle.Message>) m.invoke(null, parcelables);
        } catch (NoSuchMethodException | InvocationTargetException |
                IllegalAccessException e) {
            return null;
        }
        return list;
    }

And the error:

java.lang.IllegalArgumentException: Wrong number of arguments; expected 1, got 2
    at java.lang.reflect.Method.invoke(Native Method)
    at com.foo.bar.app.service.NotificationService.a(Unknown Source)
    at com.foo.bar.app.service.NotificationService.onNotificationPosted(Unknown Source)
    at android.service.notification.MyAppService.onNotificationPosted(MyAppService.java:237)
    at android.service.notification.MyAppService$MyHandler.handleMessage(MyAppService.java:1441)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6044)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)





Aucun commentaire:

Enregistrer un commentaire