| Built-in front calls / Genero Mobile common front calls | |
This front call registers a mobile device for push notifications.
ui.Interface.frontCall("mobile","registerForRemoteNotifications",
[sender_id], [registration_token] )
The "registerForRemoteNotifications" front call registers the mobile device for push notifications. Once the registration procedure is done (see below for platform specifics), it is possible to get notification events through the notificationpushed predefined action, and retrieve notification data with the getRemoteNotifications front call.
On iOS when using APNs, the sender_id is ignored. The registration_token is the device token returned by the Apple Push Notification service. Once registered with the Apple Push Notification service, the app must also send this device token to the push notification provider, typically using a RESTFul HTTP POST.
For more details about Apple Push Notification Provider, see Apple Push Notification Service web site.
The following code example registers with Google Cloud Messaging or Apple Push Notification service. It then sends the registration token to the push notification provider:
IMPORT com -- For RESTful post
IMPORT util -- JSON API
CONSTANT GCM_SENDER_ID = "<enter your GCM Sender ID (NULL for APNs)>"
...
DEFINE sender_id STRING,
registration_token STRING
DEFINE req com.HTTPRequest,
obj util.JSONObject,
resp com.HTTPResponse
-- First get the registration token
LET sender_id = GCM_SENDER_ID
CALL ui.Interface.frontCall(
"mobile", "registerForRemoteNotifications",
[ sender_id ], [ registration_token ] )
-- Then send registration token to push notification provider
TRY
LET req = com.HTTPRequest.create("http://SERVER_IP:4930")
CALL req.setHeader("Content-Type", "application/json")
CALL req.setMethod("POST")
CALL req.setTimeOut(5)
LET obj = util.JSONObject.create()
CALL obj.put("registration_token", registration_token)
CALL req.doTextRequest(obj.toString())
LET resp = req.getResponse()
IF resp.getStatusCode() != 200 THEN
MESSAGE SFMT("HTTP Error (%1) %2",
resp.getStatusCode(),
resp.getStatusDescription())
ELSE
MESSAGE "Registration token sent."
END IF
CATCH
MESSAGE SFMT("Could not post registration token to server: %1", STATUS)
END TRY
...