Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GCM:如何向GCM服务器发送心跳信号_Java_Android_Google Cloud Messaging_Heartbeat - Fatal编程技术网

Java GCM:如何向GCM服务器发送心跳信号

Java GCM:如何向GCM服务器发送心跳信号,java,android,google-cloud-messaging,heartbeat,Java,Android,Google Cloud Messaging,Heartbeat,我想将心跳信号从我的应用程序发送到GCM服务器,这样连接将保持活动状态 我如何做到这一点,我如何知道我的GCM服务器的URL 提前谢谢 如何发送心跳 这个类可以发送正确的意图 public class GcmKeepAlive { protected CountDownTimer timer; protected Context mContext; protected Intent gTalkHeartBeatIntent;

我想将心跳信号从我的应用程序发送到GCM服务器,这样连接将保持活动状态

我如何做到这一点,我如何知道我的GCM服务器的URL

提前谢谢

如何发送心跳 这个类可以发送正确的意图

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

    }
如果只想发送心跳信号,可以在活动中执行以下操作

GcmKeepAlive gcmKeepAlive = new GcmKeepAlive(this);
gcmKeepAlive.broadcastIntents();
我认为您不需要为此设置任何其他权限,但以下是我在清单中拥有的与gcm相关的权限

<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" />
我也有一个服务,有一个匕首注入报警马槽和悬挂帐篷

@Inject AlarmManager alarmManager;
@Inject PendingIntent gcmKeepAlivePendingIntent;


alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 4*60*1000, gcmKeepAlivePendingIntent);
以下是Dagger模块的一部分,提供报警管理器和待定意图。 有几种方法可以让报警管理器定期调用一个方法,所以假设您不使用Dagger,您仍然可以拉出相关部分。你的问题是如何发送心跳,而不是如何使用报警管理器。这已经有很多答案了,所以在上面搜索吧

@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的消息。。。有时有效,有时无效。。。知道吗?也许我不该把广播接收器放在这里。如果创建GcmKeepAlive类的实例,然后调用boadcastinents(),我会将它与报警管理器一起使用,定期发送keep-alive心跳;在上面,它应该会发出“保持活力”的心跳。我刚刚更新了我的答案。我把它分为两部分:如何发送心跳,然后是一种你可以安排定期发送心跳的方式。谢谢你的更新,但我从你的第一个答案就理解了如何发送心跳。我的问题是,即使在发送心跳信号时,我也不总是能够从GCM服务器获取数据。有时我明白,有时不明白。在您的应用程序中,推送通知是实例?
@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);
}