Push notification with custom server side

119 views
Skip to first unread message

Hugo Teixeira

unread,
May 21, 2014, 6:08:26 PM5/21/14
to codenameone...@googlegroups.com
I have a codenameone app with push notifications implemented (PushCallback + registerPush + implemented methods).
Since I am only working with Android at this point, I have a simple GCM call on the server side, like this:

String result = Request.Post("https://android.googleapis.com/gcm/send")
.addHeader("Authorization", "key=" + googleAuthKey)
.addHeader("Content-Type", "application/json")
.bodyString(msg, ContentType.APPLICATION_JSON)
.execute().returnContent().asString();

My messages are sent successfully. The problem happens when I receive them on the mobile side. For some reason, the message arrives empty/blank (I am talking about the "value" argument of the push() method implemented in the Application class). I assume the mobile code expects a specific format that I am missing on the server side.

I know I could use the codenameone server to send push notifications, but I want the simplest code on my server for now. If the codenameone server uses GCM to deliver messages, then there is no reason for my code to not work using the same format, right? So what could be wrong with my code?

By the way, I would like to get the android sources from the build server (the answers to my questions are probably there), but I don't know how to enable that from IntelliJ. Ideally this option should also be available on the "account details" section of the build server. What do you suggest?

Thanks in advance,
Hugo


Hugo Teixeira

unread,
May 21, 2014, 9:59:53 PM5/21/14
to codenameone...@googlegroups.com
I found the answers I was looking for. So let me explain the issues here:

1) The code I posted in my previous message works (that piece uses Apache HttpClient for the GCM request). The point is that we need a "message" variable in the "data" section of the JSON.
(More information about the JSON format here: http://developer.android.com/google/gcm/http.html)
My JSON looks like this now: 

{ "registration_ids":["..."], "restricted_package_name":"com.company.mobile", "data": { "message":"This is a push notification" } }

This works fine and the push notification shows up in the Android status bar without any extra code (no need to call Display.getInstance().notifyStatusBar() in this case).

2) The "build with sources" option in IntelliJ is under Preferences > CodenameOne (only for pro accounts).

Regards,
Hugo

Shai Almog

unread,
May 21, 2014, 11:35:14 PM5/21/14
to codenameone...@googlegroups.com
Hi,
while this can work in theory its something we don't officially support and you could run into problems with this approach. The notification appears because we show it, but if you don't want it to show you need to reverse engineer what we did,
Then there is iOS which is MUCH harder...
Reply all
Reply to author
Forward
0 new messages