GCM:如何将心跳发送到GCM服务器 [英] GCM: How to send heartbeat to GCM server

查看:211
本文介绍了GCM:如何将心跳发送到GCM服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序向GCM服务器发送心跳,因此连接将保持活动状态.

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

我该怎么办?我怎么知道我的GCM服务器的URL?

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

提前谢谢!

推荐答案

如何发送心跳

此类可以发送适当的意图

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();

我认为您不需要为此设置任何其他权限,但这是清单中我拥有的与gcm相关的权限

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);

此处是Dagger模块的一部分,提供警报管理器和挂起的意图. 警报管理器有几种方法可以定期调用方法,因此,假设您不使用Dagger,您仍然应该可以提取相关部分.您的问题是如何发送心跳,而不是如何使用警报管理器.已经有很多答案,因此请对其进行搜索.

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);
}

这篇关于GCM:如何将心跳发送到GCM服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆