maf push notifications

27
DOAG ADF Spotlight Webinar, June 6th 2014 ADF Mobile : Push Notifications

Upload: lucbors

Post on 05-Jul-2015

365 views

Category:

Software


11 download

DESCRIPTION

DOAG Webinar Push Notifications

TRANSCRIPT

Page 1: MAF push notifications

DOAG ADF Spotlight Webinar, June 6th 2014

ADF Mobile : Push Notifications

Page 2: MAF push notifications

Who Am I

• Luc Bors

• Principal Consultant

• AMIS, Netherlands

Page 3: MAF push notifications

Agenda

• ADF Mobile Overview Architecture

• Push Notifications Overview

• Push Notifications Cloud Messaging Part

• Push Notifications Provider Part

• Push Notifications ADF Mobile Part

Page 4: MAF push notifications

Push Notifications

Page 5: MAF push notifications

Push Notifications

Page 6: MAF push notifications

Example

• Select device

• Send message • Get notified

Page 7: MAF push notifications

Push Notifications

• Subscribe to Messaging Service

• Receive token

• Register with Enterprise app

• Enterprise app Pushes message to Messaging Service

• Messaging Service delegates message to device(s)

Page 8: MAF push notifications

Push Notification

Page 9: MAF push notifications

Setup GCM

• Google Cloud Project

• Google Cloud Messaging API is

enabled

• API key

Page 10: MAF push notifications

Setup APNS

• iOS Developer account

• App Id and Provisioning profile

– Make sure to check the Push Notification Service when you create the new App Id

• SSL Certificate

Page 11: MAF push notifications

Create a Provider Application

Page 12: MAF push notifications

Prepare Provider Application

• Add gcm-server.jar (for GCMS)

• Add javaPNS.jar (for APNS)

Page 13: MAF push notifications

Code Behind the Button

• Class to push a message to a device.

public void pushNow(ActionEvent actionEvent) {

….. Logic to get selected row…

String target = (String)curr.getAttribute("DeviceToken");

String type = (String)curr.getAttribute("DeviceType");

if (type.equalsIgnoreCase("Android")) {

pushMsgAmdroid(target, this.message);

} else {

pushMsgIos(target, this.message);

}

Page 14: MAF push notifications

Server Side for Android

• Sending the message

String sound = "default";

Message message =

new Message.Builder()

.delayWhileIdle(true)

.addData("alert", msg)

.addData("sound",sound)

.addData("FeatureName", "Sessions")

.addData("SessionId", "12")

.build();

Result result;

try {

result = sender.sendNoRetry(message, regId);

}

Page 15: MAF push notifications

Server Side for iOS

• Sending the message

/* Build a blank payload to customize */

PushNotificationPayload payload = PushNotificationPayload.complex();

/* Custom the payload */

payload.addAlert(msg);

payload.addBadge(1);

payload.addCustomDictionary("FeatureName", "Sessions");

payload.addCustomDictionary("SessionId", "12");

Push.payload(payload, KEYSTORE_LOCATION, KEYSTORE_PASSWORD, false,

target);

Page 16: MAF push notifications

Preparing ADF Mobile

• Register Application Life Cycle Listener

• LifeCycleListener must implement oracle.adfmf.application.PushNotificationConfig interface.

– This interface provides the registration configuration for push notifications

Page 17: MAF push notifications

ApplicationLifeCycleListener

• ApplicationLifeCycleListener

– In the Start() Method create a new PushNotificationListener()

public void start() {

// Add code here...

EventSource evtSource =

EventSourceFactory.getEventSource(

NativePushNotificationEventSource.

NATIVE_PUSH_NOTIFICATION_REMOTE_EVENT_SOURCE_NAME);

evtSource.addListener(new PushNotificationListener());

}

Page 18: MAF push notifications

The PushNotificationListener

• In the PushNotificationListener

– OnOpen() // receive token

– OnMessage() // handle notification

– OnError()

public void onOpen(String token) {

// Invoked during the Push Notification registration process.

// The parameter "token" contains the token received from APNs or GCMs

// that uniquely identifies a specific device-application combination.

ValueExpression ve = AdfmfJavaUtilities.getValueExpression(

"#{applicationScope.deviceToken}", String.class);

if (token != null){

ve.setValue(AdfmfJavaUtilities.getAdfELContext(), token);

}

else{

ve.setValue(AdfmfJavaUtilities.getAdfELContext(), "dummy Token");

}

}

Page 19: MAF push notifications

Token Registration

• Use a webservice to send the Token to the provider Application

• Provider stores token and uses it when sending notifications (remember the “code behind the button”

public void pushNow(ActionEvent actionEvent) {

….. Logic to get selected row…

String target = (String)curr.getAttribute("DeviceToken");

String type = (String)curr.getAttribute("DeviceType");

if (type.equalsIgnoreCase("Android")) {

pushMsgAmdroid(target, this.message);

} else {

pushMsgIos(target, this.message);

}

Page 20: MAF push notifications

All is there.

• Cloud Services are setup

• Provider app is created

• Device Token can be sent to and stored at provider app

• LETS PUSH ! And work with the notification

Page 21: MAF push notifications

Push Notifications

Page 22: MAF push notifications

onMessage()Working with the Notification

public void onMessage(Event event) {

AdfELContext adfELContext = AdfmfJavaUtilities.getAdfELContext();

JSONBeanSerializationHelper jsonHelper =

new JSONBeanSerializationHelper();

try {PayloadServiceResponse serviceResponse =

(PayloadServiceResponse)jsonHelper.fromJSON(

PayloadServiceResponse.class, event.getPayload());

ValueExpression notificationPayloadBinding =

AdfmfJavaUtilities.getValueExpression(

"#{applicationScope.notificationSessionId}", String.class);

notificationPayloadBinding.setValue(

AdfmfJavaUtilities.getAdfELContext()

, serviceResponse.getSessionId());

AdfmfContainerUtilities.gotoFeature(

"com.tamcapp.mobilebook.ses.ConferenceSessions");

}

Page 23: MAF push notifications

Badging

// also, lets decrease the application icon badge by one

int currentBadge =

AdfmfContainerUtilities.getApplicationIconBadgeNumber();

if (currentBadge > 0) {

AdfmfContainerUtilities.setApplicationIconBadgeNumber(

currentBadge - 1);

}

Page 24: MAF push notifications

In the feature…

• Use FeatureLifeCycleListener

– Activate() Method

public void activate() {

Boolean notified =

(Boolean)AdfmfJavaUtilities.evaluateELExpression(

"#{applicationScope.notified}");

if(notified.booleanValue({

AdfmfContainerUtilities.invokeContainerJavaScriptFunction(

AdfmfJavaUtilities.getFeatureName(),

"adf.mf.api.amx.doNavigation”,

new Object[] { "featureActivated" });}

}

Page 25: MAF push notifications

DEMO

Page 26: MAF push notifications

Push Notification - Summary

• Configuration can be complicated (Especially on iOS)

• Listeners, Listeners, Listeners

– ApplicationLifeCycle

– PushNotification

– FeatureLifeCycle

• Send Notification (optional with Payload)

• onMessage()

– Get Payload

– Call feature

• In Feature

– Work with payload

Page 27: MAF push notifications

Luc Bors, AMIS, The Netherlands

[email protected]

[email protected]

Follow me on : @lucb_