java-未调用GCM onHandleIntent(Intent intent)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-未调用GCM onHandleIntent(Intent intent),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3646字,纯文字阅读大概需要6分钟。
内容图文
![java-未调用GCM onHandleIntent(Intent intent)](/upload/InfoBanner/zyjiaocheng/663/5026566168d04ec4b130bcc8ab1f79bd.jpg)
我在gcmIntentService中的onHandleIntent(Intent intent)函数已经为GCM进行了注册,并为另一个将gcm令牌发送到后端db的函数调用了变量.当我运行MainActivity时,会调用GcmintentService的构造函数,而onHandleIntent不会.这是我的GcmIntentService.
public class GcmIntentService extends IntentService {
private static final String TAG = "RegIntentService";
private static final String[] TOPICS = {"global"};
String senderId = "1048700326431";
AsyncHttpClient client = new AsyncHttpClient();
public GcmIntentService() {
super(TAG);
Log.i(TAG, "ALWAYS IN HERE");
}
@Override
public void onHandleIntent(Intent intent) {
Log.i(TAG, " NOW I NEED IT IN HERE");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
// [START register_for_gcm]
// Initially this call goes out to the network to retrieve the token, subsequent calls
// are local.
// [START get_token]
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// [END get_token]
Log.i(TAG, "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(token);
// Subscribe to topic channels
subscribeTopics(token);
// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
// [END register_for_gcm]
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
/**
* Persist registration to third-party servers.
*
* Modify this method to associate the user's GCM registration token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
final RequestParams params = new RequestParams();
params.put("id", token);
client.post("", params, new JsonHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
}
public void onSuccess(int statusCode, PreferenceActivity.Header[] headers, JSONObject response) {
}
public void onFailure(int statusCode, PreferenceActivity.Header[] headers, JSONObject errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
}
/**
* Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
*
* @param token GCM token
* @throws IOException if unable to reach the GCM PubSub service
*/
// [START subscribe_topics]
private void subscribeTopics(String token) throws IOException {
for (String topic : TOPICS) {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
pubSub.subscribe(token, "/topics/" + topic, null);
}
}
}
解决方法:
我遇到了同样的问题,但是通过将其添加到清单中解决了这个问题:
<service
android:name="com.futec.h2o.RegistrationIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</service>
内容总结
以上是互联网集市为您收集整理的java-未调用GCM onHandleIntent(Intent intent)全部内容,希望文章能够帮你解决java-未调用GCM onHandleIntent(Intent intent)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。