Java – GCM: how to send heartbeat to GCM server

I want to send a heartbeat from my application to the GCM server, so the connection will remain active

How can I do this? How can I know the URL of my GCM server?

Thank you in advance!!

Solution

How to send heartbeat

This class can send the appropriate intent

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 only want to send a heartbeat, you can do the following in the activity

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

I don't think you need to set any other permissions for this, but this is my GCM related permission in the list

<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" />

A method of sending heartbeat regularly

If you want to send it regularly, that's what I do:

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 with a dagger injected alarmmanger and pendingtent

@Inject AlarmManager alarmManager;
@Inject PendingIntent gcmKeepAlivePendingIntent;


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

The following is the part of the dagger module that provides the alert manager and pending intent The alert manager has several methods that can be called periodically, so assuming you don't use dagger, you can still extract the relevant parts Your question is how to send a heartbeat, not how to use the alert manager Many answers have been searched like this

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

@Provides  AlarmManager provideGcmKeepAliveAlarmManager() {
    return (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>