Indigo Montoya
Indigo Montoya

Reputation: 57

Google Cloud Messaging with Android

For Google Cloud Messaging, does the app that is supposed to recieve notifications have to be running in the foreground or background in order to process a push notification message?

Upvotes: 0

Views: 74

Answers (2)

Arpit Ratan
Arpit Ratan

Reputation: 3026

No

Your app just needs to register for a broadcast. This typically works as follows:

There is a socket connection established between the Google server and GCM service running in your app. So when you want to send a push notification to your app you just ask Google push server to push the message to the client. Server writes the message in the socket and the service in the client's end just sends a broadcast. Your broadcast receiver on receive is invoked and then your app starts.

Advantage is that every app doesn't have to keep a constant socket connection with their server for realtime updates.

Consider reading about wake locks if you plan to use GCM and doing some heavy operation in background after receiving push.

Upvotes: 1

Evin1_
Evin1_

Reputation: 12866

It doesn't need to be running at all, that's why you add a Broadcast Receiver (GCMReceiver that will run even if your app's not active) to the Manifest . When the system receives a downstream message this Receiver will trigger a component (often a Service that will handle your incoming stream).

<receiver
     android:name="com.google.android.gms.gcm.GcmReceiver"
     android:exported="true"
     android:permission="com.google.android.c2dm.permission.SEND" >
     <intent-filter>
         <action android:name="com.google.android.c2dm.intent.RECEIVE" />
         <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
         <category android:name="YOUR_PACKAGE_NAME" />
     </intent-filter>
 </receiver>

Upvotes: 1

Related Questions