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.
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);
});
}
7 Comments
This is a great feature! Is it possible to use the rich push features with *local* notifications (described here[](https://www.codenameone.com/blog/local-notifications.html)? E.g. is it possible to call the `PushContent.get()` from within the callback `localNotificationReceived(String notificationId)`?
No. Unfortunately they are completely separate features that have no connection between them at this time.
That’s a pity, i thought local and ‘remote’ push notifications shared the same mechanics, so I hoped there was a synergy. I use local notifications for on-device popups when the app is not active, and the ‘rich’ features would be perfect to enhance that. Is it possible that the ‘rich’ features might become available for local notifications some day?
Unless an enterprise user explicitly asks for this I don’t see this happening. This was a very hard to implement RFE that took forever. We’re already backlogged with enterprise RFE’s so even if this was requested by an enterprise user it would probably take a lot of time to resolve as it’s a big task.
I’ve managed to get Push working on both Android and iOS, however my Android notifications come as silent notifications. Is there a setting where I can change them to be regular notifications?
It seems to be an issue with Android 10: https://support.google.com/pixelphone/thread/14478025?hl=en
We’re still not sure if this is something that we can handle/hide or if it’s something that Google must fix.
Good to know, thank you!