You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/developer-guide/Push-Notifications.asciidoc
+49-7Lines changed: 49 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,6 +160,8 @@ When active this will trigger the push(String) method twice, once with the visua
160
160
161
161
- `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.
162
162
163
+
- `102` - Combines badge, title, and body in a single payload formatted as `badge;title;body`. Supported on Android, iOS, and Windows.
164
+
163
165
The following sections will show examples of the various kinds of pushes. You can try them out yourself by opening the push simulator.
164
166
165
167
==== Example Push Type 1
@@ -252,6 +254,14 @@ On platforms that do not support badges, Push type 101 will behave exactly as pu
252
254
253
255
The `push()` callback will be called only if the user taps the notification. `Display.getInstance().getProperty("pushType")` will return "1" for this type.
254
256
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
+
255
265
256
266
.Badging on iOS
257
267
****
@@ -281,6 +291,20 @@ The message body should be an XML string. A minimal example of a push type 99 t
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
+
284
308
IMPORTANT: The image URL *must* be a secure URL (i.e. start with "https:" and not "http:", otherwise, iOS will simply ignore it.
285
309
286
310
.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
303
327
public void push(String message) {
304
328
PushContent content = PushContent.get();
305
329
if (content != null) {
330
+
String title = content.getTitle();
331
+
String body = content.getBody();
332
+
String hiddenMeta = content.getMetaData();
306
333
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.
309
336
}
310
337
}
311
338
----
312
339
313
340
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`.
314
341
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
+
315
344
==== Notification Actions
316
345
317
346
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 {
345
374
new PushActionCategory("invite", new PushAction[]{
346
375
new PushAction("yes", "Yes"),
347
376
new PushAction("no", "No"),
348
-
new PushAction("maybe", "Maybe")
377
+
new PushAction("maybe", "Maybe"),
378
+
new PushAction("reply", "Reply", null, "Type your response...", "Send")
349
379
})
350
380
351
381
};
352
382
}
353
383
}
354
384
----
355
385
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`.
357
387
358
388
**Sending a Push Notification with "invite" Category**
359
389
@@ -376,7 +406,7 @@ image::img/push-actions-ios.png[Push with actions on iOS,scaledwidth=30%]
376
406
.Push notification with "invite" category on the Chrome desktop includes a "More" dropdown where user can select the action.
377
407
image::img/push-actions-chrome-desktop.png[Push with actions on Chrome Desktop,scaledwidth=30%]
378
408
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()`.
380
410
381
411
The *category* of the notification will be made available via the `getCategory()` method of `PushContent`.
382
412
@@ -389,7 +419,10 @@ public void push(String message) {
389
419
if (content != null) {
390
420
if ("invite".equals(content.getCategory())) {
391
421
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");
0 commit comments