Sending GCM Notification From server — Spring Framework Java

What is GCM Noti­fi­ca­tions -

GCM stands for google cloud mes­sag­ing ser­vice,  by using this ser­vice you can send noti­fi­ca­tions to android devices even if your android app is not run­ning in there mobile that time. Wat­sApp noti­fi­ca­tions are one exam­ple

Process Flow — >

When user installs your android app in his mobile google play ser­vice gen­er­ates a GCM Reg­is­tra­tion ID which is unique for your app and that user, You need this GCM Reg Id to send noti­fi­ca­tions from serv­er.

So from your mobile app code you push this gcm reg­is­tra­tion id to the serv­er, and stores it in the data­base record asso­ci­at­ed with that user.

Now you call GCM Api with pass­ing that gcm reg­is­tra­tion id & your key-val­ue data (like {“message”:“test”,navigateTo:“news”})

It will ping user‘s mobile with that noti­fi­ca­tion. (maybe android devel­op­er need to write code too to han­dle these para­me­ters, but i have no idea as i am not android devel­op­er)

 

Cod­ing:

Include Google’s gcm-serv­er library using maven depen­den­cy  — >

Now you need to cre­ate new sender object -

Cre­ate Mes­sage:
Send the mes­sage :

All Code At Once:

 

Thats it,

vivek

Web Developer & Server Admin, Skilled in Java , PHP , LAMP, Tomcat, Mongo DB & SQL. Available for freelancing project or Open Source Contribution, Feel free to contact me at contact@viveksoni.net .

You may also like...

  • Bri­an Brown

    Thank you Vivek, this was very help­ful.

    How­ev­er, I noticed a small mis­take. You cre­at­ed two mes­sages but only used one of them.

    • I am glad to help you. Thank you for the point­ing mis­take, will fix that.

  • anil kumar Pandey

    Yes, Final­ly I found a good gcm stuff with sping mvc 🙂 Great work Vivek.

  • anil kumar Pandey

    Hi Vivek, I did exact­ly what you men­tioned above ser­vice class but I am get­ting this Account cannot be resolved to a type in my eclipse. Actul­ly GCM is pret­ty new for me, so please help me to out of it. Can you please make a github repo for the same, it will help oth­ers also.

    • oops, That Account object is rep­re­sent­ing data­base enti­ty where user details are stored, so i am using account object for stor­ing gcm­Reg Id only.

      • anil kumar Pandey

        Ohh yes, now its work­ing fine 🙂 but one more thing vivek, right now the code expect­ing only one “gcm­RegId” but what approach I need to use when I have 100‑1000 or more gcm reg ids. I hope you are get­ting my point. Please give me some sug­ges­tion on it.

        • if you want to send bulk noti­fi­ca­tions then pass gcm­RegId as List .

          for exam­ple-

          send(Message mes­sage, List regIds, int retries)

          i have added com­ment in code snipt

          • anil kumar Pandey

            Ok, great. Thanks Its work­ing, just I need to change the return type “Result” to “Mul­ti­cas­tRe­sult”. Thanks for your sup­port.

  • Sopheak Seng

    Hi Vivek, do you have any java exam­ple if I want to send noti­fi­ca­tion to client via web brows­er (Web­Site), not to Android?

    • easy & best way for imple­ment­ing web noti­fi­ca­tion is https://onesignal.com/ , use their APIs its free and per­fect.

      • Sopheak Seng

        Nice, thank you