4

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!!

Itay
  • 219
  • 5
  • 14

1 Answers1

9

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);
}
nPn
  • 16,254
  • 9
  • 35
  • 58
  • 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 08:50
  • So are you saying you already knew how to send the heart beat? Or did this answer that question and now you have a new question? – nPn Dec 15 '14 at 12:56
  • Well, I asked that question because of a research that I did, that tells that to solve the instant problem, I need to send heartbeat to the GCM server. Your code didn't solved the problem, so I'm not sure that thus is the way to send the heartbeat.... Did it solve it for you? – Itay Dec 15 '14 at 20:20
  • Based on my Logcat's the way to tell if the heartbeat is being sent is to look for a message in the log that says has the string com.google.e.a.a.h ... i see that within 500ms of the heartbeat. – nPn Dec 16 '14 at 03:25
  • If the heart beat is not your problem, then you can take a look at http://developer.android.com/google/gcm/server.html#params, i set time_to_live to 0 and delay_while_idle to false, there also might be something in the collapse_key. – nPn Dec 16 '14 at 03:45
  • And yes, the code I have shown above, solved the problem of not getting reliable gcm messages from my server. It is always possible that you have other issues beyond the scope of the question you asked. Hopefully I helped point you in the right direction - good luck! – nPn Dec 16 '14 at 03:51
  • Should I add anything to the Manifest for your code? – Itay Dec 16 '14 at 10:06
  • And if the time_to_live be 0, the message won't be sent to device if it's offline, is it? – Itay Dec 16 '14 at 15:35
  • right, but for some apps that is OK since it's more of a now or never requirement ... ie if you are requesting the device to send it's location to the server you might only be interested in the location at that moment. – nPn Dec 16 '14 at 16:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67034/discussion-between-itay-and-npn). – Itay Dec 16 '14 at 17:07