B4A Library Google Cloud Messaging Server\Visual Studio

walterf25

Expert
Licensed User
Hello All, i'm not sure if this will be of any help to anyone, but i decided to share it with everyone here anyway. I'm writing an application for a client and needed an easy way to test out a portion of the app which requires the Google Cloud Messaging system, so after searching for a while for an easy way for me to do it, i decided to write this application in Visual Basic 2008, Erel's tool in his Tutorial is a great tool but i just don't like the fact that i had to open a command window every time and type in the command to send a message.

Anyhow with this tool you only need to type the API Key provided by google once, along with the Sender ID which is the number provided when you log in into your Google API, and the Registration ID, you get this when you run your application.

This is the application written in Visual Studio 2008
GCM_Server.png Message.jpg

the SenderID is this number you receive from google
SenderID.jpg

Your Api Key you get it from here
api.jpg

Your Registration ID is the number generated when you run your application
check Erel's tutorial for this
http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/19226-android-push-notification-gcm-framework-tutorial.html#post110713

Again once you enter all this information for the first time on the required fields, this information gets saved into a config.txt file the next time you launch the VB GCM server it will load the saved api, senderId and registration id information.

Hope you guys find this useful!
View attachment GCM_Server executable.zip

And Here's the source code, please feel free to play with it, if anyone expands the functionality of it please don't forget to post your changes on this thread, so that others can benefit from this as well!
View attachment GCM_Server_Source_code.zip


Cheers!
 
Last edited:

imbault

Well-Known Member
Licensed User
Hi, could you share Visual basic source code please, in order to integrate your code in an existing application (VB web services)
Thanks.
Patrick
 

walterf25

Expert
Licensed User
Hi All i've added the source code on the first post, please feel free to make changes but don't forget to post back on this thread so that others can benefit from this as well!

Enjoy!
Walter
 

JakeBullet70

Well-Known Member
Licensed User
More good stuff to play with!!!
 

SueHale

Member
Licensed User
Hi Walter,

Thank-you for sharing the VB code you have created. Have been trying to find where my Registration ID is shown/generated.
On running the B4A Example no code is shown. I know am missing the obvious but have spent a while....

Best wishes
Sue.
 

walterf25

Expert
Licensed User
Hi Walter,

Thank-you for sharing the VB code you have created. Have been trying to find where my Registration ID is shown/generated.
On running the B4A Example no code is shown. I know am missing the obvious but have spent a while....

Best wishes
Sue.
Hi SueHale, you can find the registration ID on Erel's gcmpush sevice module provided on his tutorial, you can find it by looking at this code
B4X:
Sub HandleRegistrationResult(Intent As Intent)
    If Intent.HasExtra("error") Then
        'Log("Error: " & Intent.GetExtra("error"))
        Dim s_error As String
        s_error = Intent.GetExtra("error")
        If (s_error = "ACCOUNT_MISSING") Then
            Dim i As Intent               
            i.Initialize("", "")
            i.SetComponent("com.google.android.gsf/.login.AccountIntroActivity")
            StartActivity(i)
        Else
            ToastMessageShow("Error: " & s_error, True)   
            'Log("error from registration :" & s_error)
            'CallSubDelayed2(Main, "error", s_error)
        End If
       
    Else If Intent.HasExtra("unregistered") Then
    'do nothing
    Else If Intent.HasExtra("registration_id") Then
        'If hcInit = False Then hc.Initialize("hc")
        Dim rid As String
        rid = Intent.GetExtra("registration_id")    'Registration ID
        Log("Registradion ID = " & rid)
End If
End Sub
cheers,
Walter
 

SueHale

Member
Licensed User
Hi Walter,

Thank-you for your code snippet to handle the registration ID. That worked well. When I use your code I end up getting a quite large response back from Goggle. No message appears apart from the Toast to say New Message.Over the weekend will have another look at this and start again from first base and the code Erel provided in the original tutorial. Great technology and combined with your nice VB app will be very handy.

Regards
Sue.
 

Attachments

walterf25

Expert
Licensed User
Hi SueHale the long message you receive when you send a message is normal, that is the normal response from google cloud server, as far as you not seeing the message on your screen is because you have to implement that in your code.

Cheers,
Walter
 

SueHale

Member
Licensed User
Hi Walter,

OK about receiving back a long message from Google Cloud. Will go back to the original sample and see what has changed. Kind of thought that the Sub MessageArrived would have just displayed the message without change. Plenty of reading for me to do.
 

walterf25

Expert
Licensed User
Hi Walter,

OK about receiving back a long message from Google Cloud. Will go back to the original sample and see what has changed. Kind of thought that the Sub MessageArrived would have just displayed the message without change. Plenty of reading for me to do.
Hahaha, if you want to display the message, you will have to get the message received from the MessageArrived sub and either display it in a toast or you can create a transparent window like in my example and display the window with the message on it, or you can also display it on the notification bar.

Cheers,
Walter
 

SueHale

Member
Licensed User
I feel is the issue, is no message to receive, sure can display a toast to say is a message. The message is not getting through.
I think better for me to read up on the process from start to finish and not burden your time further.

Regards
Sue.
 

walterf25

Expert
Licensed User
I feel is the issue, is no message to receive, sure can display a toast to say is a message. The message is not getting through.
I think better for me to read up on the process from start to finish and not burden your time further.

Regards
Sue.
Take a look at the code below, specifically the MessageArrived sub

B4X:
Sub MessageArrived (Intent As Intent)
'Log("extras " & Intent.ExtrasToString)
Try

    If Intent.HasExtra("from") Then From = Intent.GetExtra("from")
    'Log(From)
    If Intent.HasExtra("message") Then
    Data = Intent.GetExtra("message")
    Log("Received " & Data) ' this is the message received
    StartActivity(Messages)
    End If
    If Intent.HasExtra("collapse_key") Then CollapseKey = Intent.GetExtra("collapse_key")
    'Log(CollapseKey)
Catch
    Log(LastException.Message)
End Try
   
End Sub
Notice the part where it says
"Data = Intent.GetExtra("message")
it needs to be "message" not anything else.

this retrieves the message you sent with the visual studio program.

Hope this helps.

Walter
 

SueHale

Member
Licensed User
Hi Walter,

Thank-you for the additional code. That was the exact but now very obvious part had over looked!

With regards
Sue.
 

Attachments

Reinosoft

Member
Licensed User
Hi Walter, Great code! This is the example i was looking for!
a desktop programm without java, but pure code, great job!

i've got a question, is there a way to use GCM when i'm not connected to Wifi, but eg at 3G Mobile Network?

Thanks Reinald
 

walterf25

Expert
Licensed User
Hi Reinosoft, i'm sure it could be done but i haven't looked into it, i'm very busy with other projects, but will try to look into it once i have some free time.

Cheers,
Walter
 
Top