Fork us on GitHub

Rich Push Notifications Improved

What happened in the build cloud migration and how it will impact users
Rich Push Notifications Improved

Rich Push Notifications Improved

Last week Steve committed the final piece of the rich push notification support RFE. This commit introduces support for replies in push messages. This came too late for the whatsapp clone but if you want to build an app of this type you would need this feature.

The app main class should implement PushActionProvider. This defines a method that returns a set of categories. E.g.

public PushActionCategory[] getPushActionCategories() {
    return new PushActionCategory[]{
        new PushActionCategory("fo", new PushAction[]{
            new PushAction("yes", "Yes"),
            new PushAction("no", "No"),
            new PushAction("maybe", "Maybe", null, "Enter reason", "Reply")
        })

    };
}

Then, when sending a push notification, you can specify the "category" of the message. If the category corresponds with a defined category in your getPushActionCategories() method, then the user will be presented with a set of buttons corresponding to the PushActions in that category.

In the above example, we would send a push type 99 and a body of

<push type="0" body="Hello" category="fo"/>

This would trigger the "fo" category that we defined, which has 3 actions: Yes, No, and Maybe. And the "Maybe" action will provide a text input because of the extra parameters provided:

new PushAction("maybe", "Maybe", null, "Enter reason", "Reply")

The last 2 parameters are the "hint" text and the reply button label. On android, the notification will look like this.

Push Reply
Figure 1. Push Reply

If you click on "Maybe" (with Android API level 27 or higher which is the default), then you’ll get a text field to enter a reply directly.

You can retrieve both which action was pressed, and what the user text input was using the PushContent class.

An example push callback method to retrieve this data:

public void push(String value) {
    PushContent data = PushContent.get();
    if (data != null) {
        Log.p("Image URL: "+data.getImageUrl());
        Log.p("Category: "+data.getCategory());
        Log.p("Action: "+data.getActionId());
        Log.p("Text Response: "+data.getTextResponse());
    } else {
        Log.p("PushContent is null");
    }
    Log.p("Push "+value);
    Display.getInstance().callSerially(()->{
        Dialog.show("Push received", value, "OK", null);
    });
}
Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.