I want to send a heartbeat from my application to the GCM server, so the connection will stay alive.

How can I do that, and how can I know the URL of my GCM server??

Thanks in advance!!

up vote 6 down vote accepted

How to send the heartbeat

This class can sent the proper intents

   public class GcmKeepAlive  {

        protected CountDownTimer timer;
        protected Context mContext;
        protected Intent gTalkHeartBeatIntent;
        protected Intent mcsHeartBeatIntent;

        public GcmKeepAlive(Context context) {
            mContext = context;
            gTalkHeartBeatIntent = new Intent(
                    "com.google.android.intent.action.GTALK_HEARTBEAT");
            mcsHeartBeatIntent = new Intent(
                    "com.google.android.intent.action.MCS_HEARTBEAT");  
        }

        public void broadcastIntents() {
            System.out.println("sending heart beat to keep gcm alive");
            mContext.sendBroadcast(gTalkHeartBeatIntent);
            mContext.sendBroadcast(mcsHeartBeatIntent);
        }

    }

if you just want to send the heartbeat you can do the following in an Activity

GcmKeepAlive gcmKeepAlive = new GcmKeepAlive(this);
gcmKeepAlive.broadcastIntents();

I don't think you need to set any additional permissions for this but here are the gcm related permissions I have in my manifest

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
    android:name=your_package_name.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="your_package_name.permission.C2D_MESSAGE" />

One way to send the heartbeats on a regular basis

If you want to send them on a regular basis, here is how I am doing that:

    public class GcmKeepAliveBroadcastReceiver extends BroadcastReceiver {

        private GcmKeepAlive gcmKeepAlive;

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("inside gcm keep alive receiver");
            gcmKeepAlive = new GcmKeepAlive(context);
            gcmKeepAlive.broadcastIntents();

        }

    }

I also have a service that has an Dagger injected alarmmanger and pendingintent

@Inject AlarmManager alarmManager;
@Inject PendingIntent gcmKeepAlivePendingIntent;


alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 4*60*1000, gcmKeepAlivePendingIntent);

Here is the section of the Dagger module that provides the alarm manager and pending intent. There are several ways to have an alarm manager periodically call a method, so assuming you don't use Dagger, you should still be able to pull out the relevant parts. Your question was how to send the heartbeat, not how to use an alarm manager. There are lots of answers to that already so search on that.

@Provides PendingIntent provideGcmKeepAlivePendingIntent() {
    System.out.println("pending intent provider");
    Intent gcmKeepAliveIntent = new Intent("com.gmail.npnster.first_project.gcmKeepAlive");
    return PendingIntent.getBroadcast(mContext, 0, gcmKeepAliveIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}

@Provides  AlarmManager provideGcmKeepAliveAlarmManager() {
    return (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
}
  • Thanks! I'll check it soon! – Itay Dec 14 '14 at 16:46
  • I put that code inside button to refresh, and it still not getting the messageges from the GCM al the time... Sometimes it work and sometimes not... Any idea? – Itay Dec 14 '14 at 22:21
  • maybe I should not have included the broadcast receiver here. I use it in along with an alarm manager to send the keep alive heartbeats on a regular basis If you create an instance of the GcmKeepAlive class and then call boadcastIntents(); on it, it should send the keep alive heartbeat. – nPn Dec 14 '14 at 22:42
  • I just updated my answer. I broke it into two parts how to send the heartbeat and then one way in which you can arrange to have the heartbeat sent on a regular basis. – nPn Dec 14 '14 at 23:07
  • Thanks for your update, but I understood how to send the heartbeat from your first answer. My problem is that even when I send the heartbeat I'm not always get the data that I should get from the GCM server. Sometimes I get it, and sometimes not. In your app the push notifications is instance? – Itay Dec 15 '14 at 8:50

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.