This question might be stupid but I'll try anyway... I have a some webservices running on grails framework. Currently, my clients (android, iOS and windows phone devices) communicate with those services via classic http get/post requests. I need to add a new service to push some notifications to clients in a JSON format (keeping connection alive is NOT an option - nor is regular polling), the tricky part is that I'd like the service to be as independent as possible from the client platform (therefore, tools like Google Cloud Messaging and Apple Push Notification Service cannot be used)... Any suggestions on how to do that? And is this even possible to achieve?
Asked
Active
Viewed 1,183 times
2 Answers
3
I think some plugins of Grails is not stable. For Android, you can do http post using scribe framework. Example:
def pushToAndroidDevice(String deviceToken, Map data) {
OAuthRequest request = new OAuthRequest(Verb.POST, 'https://android.googleapis.com/gcm/send')
request.addHeader("Authorization", "key=" + apiKey)
request.addHeader("Content-Type", "application/json")
def jsonBody = [:]
def deviceTokens = [] << deviceToken
jsonBody.registration_ids = deviceTokens
jsonBody.data = data
request.addPayload((jsonBody as JSON).toString())
Response response = request.send()
return JSON.parse(response.body).toString()
}
For iOS, you can use library javaPNS: https://code.google.com/p/javapns/
import javapns.Push;
....
def pushToIOSDevice(String deviceToken, Map data) {
String certificatePath = grailsConfig.apns.certificate //path of your certificate (*.p12)
String password = grailsConfig.apns.password //your apns password
PushNotificationPayload payload = PushNotificationPayload.complex();
payload.addAlert(data.get("message"));
payload.addCustomDictionary("title", data.get("title"));
payload.addCustomDictionary("action", data.get("action"));
Push.payload(payload, certificatePath, password, false, deviceToken);
}

Dat Nguyen
- 1,881
- 17
- 36
0
When you said http get/post
requests from android, iOS and windows phone
I am assuming you are talking about their browsers. In such a case, you can use WebSocket which is supported by major browsers to achieve the same.
Atmosphere is what I would vouch for in this case.

dmahapatro
- 49,365
- 7
- 88
- 117
-
Actually, I am using some apache libraries to fire my requests... But the Atmosphere project is pretty much what I was looking for, thanks! – edhoedt Jun 13 '13 at 20:04