DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Simplify Your Compliance With Google Cloud Assured Workloads
  • How To Build Translate Solutions With Google Cloud Translate AI
  • Building and Deploying a Chatbot With Google Cloud Run and Dialogflow
  • Provision Cloud Infrastructure Using Google Duet AI

Trending

  • Chaos Engineering for Microservices
  • Why Documentation Matters More Than You Think
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • A Modern Stack for Building Scalable Systems
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Google Cloud Messaging with Payload

Google Cloud Messaging with Payload

By 
Tony Siciliani user avatar
Tony Siciliani
·
Updated Oct. 11, 22 · Interview
Likes (1)
Comment
Save
Tweet
Share
22.7K Views

Join the DZone community and get the full member experience.

Join For Free

google cloud messaging (or gcm) sends two types of messages:

  1. collapsible, “send-to-sync” messages, where  new messages replace older ones  in the sending queue. (i.e. the older messages are “collapsed”).
  2. non-collapsible messages with payload, where  every single message is delivere  d.

each payload in non-collapsible messages is a unique content that has to be delivered and can’t be just replaced with a more recent message in the server sending queue. on the other hand, a  collapsible message can be a simple  ping  from the server to ask its mobile clients to sync their data.

when to use messages with payload? instant messaging (im) applications come to mind. another use case is when we need to include data into our push notifications to save our clients a round-trip to the server. an example use case would be sending daily/monthy online game rankings of top players. instead of just notifying the android clients to go to the server to get the information (  send-to-sync  ), the data is included in the multicast messages themselves so that they can be directly consumed by the clients. we can easily see why collapsible messaging wouldn’t make much sense here, since we want our users to receive every single ranking we send them at the end of every week/month.

let’s now jump into coding. as suggested in the two previous article, these code examples are modifications of the  gcm demo application  available for download (client + server) and using the  gcm helper library for java  .

server code

creating a message with payload in server code is  similar to the process  for the collapsible type, except that we simply omit the collapse_key parameter.

// in imports
import com.google.android.gcm.server.message;

// inside send method, construct a message with payload
message message = new message.builder()
 .delaywhileidle(true) // wait for device to become active before sending.
 .adddata( "rankings", "top 5 high game scorers" )
 .adddata( "1", "1. yankeedoodle (15 trophies)" )
 .adddata( "2", "2. billy_the_kid (13 trophies)" )
 .adddata( "3", "3. viper (10 trophies)" )
 .adddata( "4", "4. silversurfer (9 trophies)" )
 .adddata( "5", "5. gypsy (8 trophies)" )
 .build();

client code

the client just retrieves the data by its keys:

// inside gcmintentservice
@override
protected void onmessage(context context, intent intent) {
  // message with payload
  string message = intent.getstringextra("rankings")  + "\n"
                 + intent.getstringextra("1")+ "\n"
                 + intent.getstringextra("2")+ "\n"
                 + intent.getstringextra("3")+ "\n"
                 + intent.getstringextra("4")+ "\n"
                 + intent.getstringextra("5");

  displaymessage(context, message);
  // notifies user
  generatenotification(context, message);
}

this is how it looks like on an android device:

these messages can contain a payload limit of  4k  of data so they are not indicated for applications that need to send more than that, although it is theoretically possible to get around that limit with multiple messages and some assembly work  on the receiving android client application. another aspect to consider would be performance and impact on the handset’s batteries, since messages with payload are not as lightweight as their collapsible cousins. regardless of the message type,  all payload data must be string values  .  so if we need to send some other type, it is up to our application to properly encode/decode the content.

Payload (computing) Google (verb) Cloud

Published at DZone with permission of Tony Siciliani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Simplify Your Compliance With Google Cloud Assured Workloads
  • How To Build Translate Solutions With Google Cloud Translate AI
  • Building and Deploying a Chatbot With Google Cloud Run and Dialogflow
  • Provision Cloud Infrastructure Using Google Duet AI

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!