More Related Content Similar to How do I - Use Push Notification.pdf (20) More from ShaiAlmog1 (20) How do I - Use Push Notification.pdf3. What’s Push?
✦Originally iOS didn’t allow processes to run in the
background (it’s still pretty hard there)
✦Push lets the server notify the user about an update
✦Mostly a visual notification, badge or message
✦It’s different on Android, but we try to “normalize” that
✦Push can be disabled by the user & won’t be available
on Kindle where Google play services aren’t available
© Codename One 2017 all rights reserved
4. How does it work?
✦Each OS has a radically different implementation
of push
✦We have our own servers that mitigate that by
exposing a unified web service call for iOS,
Android, Windows, Chrome & Firefox
✦We still need credentials to work with each one of
the server types I’ll cover only iOS & Android for
simplicity
© Codename One 2017 all rights reserved
18. public class MainClass implements PushCallback {
public void init(Object context) {
// …
}
public void start() {
// …
Display.getInstance().callSerially(() ->
Display.getInstance().registerPush());
}
public void push(String value) {
// …
}
public void registeredForPush(String deviceId) {
}
public void pushRegistrationError(String error, int errorCode) {
Log.p("Push registration failed: " + error);
}
Push Callback
19. Push Callback
public class MainClass implements PushCallback {
public void init(Object context) {
// …
}
public void start() {
// …
Display.getInstance().callSerially(() ->
Display.getInstance().registerPush());
}
public void push(String value) {
// …
}
public void registeredForPush(String deviceId) {
}
public void pushRegistrationError(String error, int errorCode) {
Log.p("Push registration failed: " + error);
}
20. Push Callback
public class MainClass implements PushCallback {
public void init(Object context) {
// …
}
public void start() {
// …
Display.getInstance().callSerially(() ->
Display.getInstance().registerPush());
}
public void push(String value) {
// …
}
public void registeredForPush(String deviceId) {
}
public void pushRegistrationError(String error, int errorCode) {
Log.p("Push registration failed: " + error);
}
21. Sending Push
✦ From another mobile device you can use the
Push class
✦ From a server send a standard POST request as
explained in the developer guide
© Codename One 2017 all rights reserved
22. Send Push
HttpURLConnection connection = (HttpURLConnection)new URL("https://
push.codenameone.com/push/push").openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-
urlencoded;charset=UTF-8");
String query = "token=" + PUSH_TOKEN +
"&device=" + URLEncoder.encode(deviceId, "UTF-8") +
"&type=" + type +
"&auth=" + URLEncoder.encode(GCM_SERVER_API_KEY, "UTF-8") +
"&certPassword=" + URLEncoder.encode(pass, "UTF-8") +
"&cert=" + URLEncoder.encode(cert, "UTF-8") +
"&body=" + URLEncoder.encode(messageBody, "UTF-8") +
"&production=" + ITUNES_PRODUCTION_PUSH;
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes("UTF-8"));
}
int c = connection.getResponseCode();
23. Types of Push Messages
✦Other types are available for special cases
✦0 or 1 - The default push. It will appear as a visible
message to the user
✦2 - invisible push, doesn’t show any UI and only invokes
the push method. Will not work in the background in iOS
✦3 - Combines 1 and 2 into a single bundled message.
When a visual message is clicked you receive it & the
hidden message
24. Troubleshooting Push
✦Try push from the simulator, check the network
inspector to verify the request/response
✦Inspect the JSON result, it should include error
messages if there is a problem
✦For iOS push make sure the certificate are accessible
as a file and not as a page with a download button
✦Verify production/sandbox modes in iOS. IOS devices
take 24hr to switch between production & sandbox
© Codename One 2017 all rights reserved