Registering Listeners for Push Notifications

(Optional) The application should register and implement a listener interface to receive native or payload push notifications.

To enable push notifications, include the gcm.jar available at SUP_HOME\MobileSDK<Version>\OData\Android\libraries\ in your application project. For more information, refer to the information available in http://developer.android.com/guide/google/gcm/gs.html .

The IODPPushNotificationListener interface should be implemented by the application to receive the GCM native push notifications on Android devices. The registerForNativePush method in the ODPClientConnection class is used to register native push notifications. The onGCMNotification method is called when a new GCM push notification is received. For more information on configuring GCM native notifications on SCC, see GCM Native Notification Properties in Sybase Control Center for Sybase Unwired Platform.

Note: When the application is not running or terminated, the GCM notifications will not reach the client device. The IODPPushNotificationListener will not be invoked.

The following code illustrates how to register listeners for native push notifications.

public class MyPushListener implements IODPPushNotificationListener {
public int onGCMNotification(Hashtable hashValues) {
// Utilize notification
log.i(“GCM Notification received”);
Return 0;
}
MyPushListener myPushListener = new MyPushListener();
ODPClientConnection.getInstance().registerForNativePush (myPushListener);
}
The return values for onGCMNotification include:
  • 0 – The client receives the payload notification from the server, if the online/payload push with native notification option is selected in the SCC Push Configurations tab. For more information, see Configuring Native Notifications in Sybase Control Center for Sybase Unwired Platform.
  • 1 – The client does not receive the payload notification from the server.

To consume push messages, the application registers a listener object.The client SDK notifies this listener object whenever there is a push message from the server. The listener object should implement the ISDMNetListener interface.

The following code illustrates how to enable Android push notification on a device.
public class MyPushListener implements ISDMNetListener{
@Override
public void onError(ISDMRequest arg0, ISDMResponse arg1,
ISDMRequestStateElement arg2) {
Log.i("Error receiving push notification");
}

@Override
public void onSuccess(ISDMRequest arg0, ISDMResponse arg1) {
// TODO Auto-generated method stub
Log.i("Push notification received successfully");
}
}
MyPushListener myPushListener = new MyPushListener();
ODPClientConnection.getInstance().registerForPayloadPush(myPushListener);
}