Skip to content

Commit c3faa2b

Browse files
authored
Document push builder enhancements and new payload type (#4067)
1 parent 5190260 commit c3faa2b

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

docs/developer-guide/Push-Notifications.asciidoc

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ When active this will trigger the push(String) method twice, once with the visua
160160

161161
- `101` - identical to 100 with an added message payload separated with a space. E.g. `30 You have 30 unread messages` will set the badge to "30" and present the push notification text of "You have 30 unread messages". Supported on Android, iOS, and Windows.
162162

163+
- `102` - Combines badge, title, and body in a single payload formatted as `badge;title;body`. Supported on Android, iOS, and Windows.
164+
163165
The following sections will show examples of the various kinds of pushes. You can try them out yourself by opening the push simulator.
164166

165167
==== Example Push Type 1
@@ -252,6 +254,14 @@ On platforms that do not support badges, Push type 101 will behave exactly as pu
252254

253255
The `push()` callback will be called only if the user taps the notification. `Display.getInstance().getProperty("pushType")` will return "1" for this type.
254256

257+
==== Example Push Type 102
258+
259+
**Push Type 102; Message Body "5;Hello World;You have 5 new tasks"**
260+
261+
Push type 102 allows you to set the badge, title, and body in a single payload. The first segment (before the first semicolon) is the badge value, the second segment is the notification title, and the remainder after the second semicolon is the body that will be displayed to the user.
262+
263+
On platforms that do not support badges or titles, the payload will fall back to the features that are supported. When your `push()` callback is invoked it will receive the `title;body` portion ("Hello World;You have 5 new tasks" in this example).
264+
255265

256266
.Badging on iOS
257267
****
@@ -281,6 +291,20 @@ The message body should be an XML string. A minimal example of a push type 99 t
281291
<push type="1" body="Hello World"><img src="https://exmaple.com/myimage.jpg"/></push>
282292
----
283293

294+
To avoid hand-coding this XML you can use the `com.codename1.push.PushBuilder` helper which assembles the payload for you and returns the correct push type to send:
295+
296+
[source,java]
297+
----
298+
PushBuilder builder = new PushBuilder()
299+
.type(1)
300+
.body("Hello World")
301+
.imageUrl("https://example.com/myimage.jpg");
302+
String payload = builder.build();
303+
int pushType = builder.getType(); // Will be 99 when image/category metadata is present
304+
----
305+
306+
You can then send `payload` as the message body with `pushType` as the type via the REST API, the `Push` helper, or any of the other examples below.
307+
284308
IMPORTANT: The image URL *must* be a secure URL (i.e. start with "https:" and not "http:", otherwise, iOS will simply ignore it.
285309

286310
.Push type 99 with attached image in simulator.
@@ -303,15 +327,20 @@ You can access additional information about the push content using the `com.code
303327
public void push(String message) {
304328
PushContent content = PushContent.get();
305329
if (content != null) {
330+
String title = content.getTitle();
331+
String body = content.getBody();
332+
String hiddenMeta = content.getMetaData();
306333
String imageUrl = content.getImageUrl();
307-
// The image attachment URL in the push notification
308-
// or `null` if there was no image attachment.
334+
String replyText = content.getTextResponse();
335+
// Use these values (title/body/hiddenMeta/imageUrl/replyText) as needed in your application logic.
309336
}
310337
}
311338
----
312339

313340
WARNING: Make sure to only call `PushContent.get()` *once* inside your `push()` callback, and store the return value. `PushContent.get()` works like a queue of size=1, and it pops off the item from the front of the queue when it is called. If you call it twice, the second time will return `null`.
314341

342+
`PushContent` exposes all of the fields that might accompany a rich notification: `getTitle()`/`getBody()` for the visible text, `getMetaData()` for hidden metadata (such as the second segment of a type 3 payload), `getImageUrl()` for attachments, `getCategory()`/`getActionId()` for actions, and `getTextResponse()` for any user-entered reply text.
343+
315344
==== Notification Actions
316345

317346
When you include actions in a push notification, the user will be presented with buttons as part of the notification on supported platforms. E.g. if the notification is intended to invite the user to an event, you might want to include buttons/actions like "Attending", "Not Attending", "Maybe", so that the user can respond quickly to the notification and not necessarily have to open your app.
@@ -345,15 +374,16 @@ public class MyApplication implements PushCallback, PushActionsProvider {
345374
new PushActionCategory("invite", new PushAction[]{
346375
new PushAction("yes", "Yes"),
347376
new PushAction("no", "No"),
348-
new PushAction("maybe", "Maybe")
377+
new PushAction("maybe", "Maybe"),
378+
new PushAction("reply", "Reply", null, "Type your response...", "Send")
349379
})
350380
351381
};
352382
}
353383
}
354384
----
355385

356-
In the above example, we create only a single category, "invite" that has actions "yes", "no", and "maybe".
386+
In the above example, we create only a single category, "invite" that has actions "yes", "no", "maybe", and a "reply" action that prompts the user for text input. A text-input action is enabled by providing either a placeholder, button text, or both when constructing the `PushAction`.
357387

358388
**Sending a Push Notification with "invite" Category**
359389

@@ -376,7 +406,7 @@ image::img/push-actions-ios.png[Push with actions on iOS,scaledwidth=30%]
376406
.Push notification with "invite" category on the Chrome desktop includes a "More" dropdown where user can select the action.
377407
image::img/push-actions-chrome-desktop.png[Push with actions on Chrome Desktop,scaledwidth=30%]
378408

379-
The `push()` callback will be fired after the user taps on the notification, or one of its actions. If the user taps the notification itself, and not one of the actions, then your `PushContent.getActionId()` will return `null`. If they selected one of the actions, then the action ID of that action can be obtained from `getActionId()`.
409+
The `push()` callback will be fired after the user taps on the notification, or one of its actions. If the user taps the notification itself, and not one of the actions, then your `PushContent.getActionId()` will return `null`. If they selected one of the actions, then the action ID of that action can be obtained from `getActionId()`. For text-input actions, the user's reply text is returned by `PushContent.getTextResponse()`.
380410

381411
The *category* of the notification will be made available via the `getCategory()` method of `PushContent`.
382412

@@ -389,7 +419,10 @@ public void push(String message) {
389419
if (content != null) {
390420
if ("invite".equals(content.getCategory())) {
391421
if (content.getActionId() != null) {
392-
System.out.println("The user selected the "+content.getActionid()+" action");
422+
System.out.println("The user selected the "+content.getActionId()+" action");
423+
if (content.getTextResponse() != null) {
424+
System.out.println("They replied: "+content.getTextResponse());
425+
}
393426
} else {
394427
System.out.println("The user clicked on the invite notification, but didn't select an action.");
395428
}
@@ -664,7 +697,16 @@ if(ITUNES_PRODUCTION_PUSH) {
664697
cert = ITUNES_PRODUCTION_PUSH_CERT;
665698
pass = ITUNES_PRODUCTION_PUSH_CERT_PASSWORD;
666699
}
667-
new Push(PUSH_TOKEN, "Hello World", deviceKey)
700+
701+
PushBuilder builder = new PushBuilder()
702+
.type(102)
703+
.badge(5)
704+
.title("Hello World")
705+
.body("You have 5 new tasks")
706+
.metaData("taskListId=123");
707+
708+
new Push(PUSH_TOKEN, builder.build(), deviceKey)
709+
.pushType(builder.getType())
668710
.apnsAuth(cert, pass, ITUNES_PRODUCTION_PUSH)
669711
.gcmAuth(FCM_SERVER_API_KEY)
670712
.wnsAuth(WNS_SID, WNS_CLIENT_SECRET)

0 commit comments

Comments
 (0)