Android的 - 没有得到GCM令牌 [英] Android - not getting GCM token

查看:203
本文介绍了Android的 - 没有得到GCM令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我称之为 RegistrationIntentService

 如果(checkPlayServices()){
        Log.i(udazzT,检查servicies);
        //开始IntentService注册该应用程序GCM。
        意图int​​ent2 =新的意图(这一点,RegistrationIntentService.class);
        startService(intent2);
    }
 

但我看不到任何日志中RegistrationIntentService:

 公共类RegistrationIntentService扩展IntentService {

    私有静态最后字符串变量=RegIntentService;
    私有静态最后的String []主题= {全球};

    公共RegistrationIntentService(){
        超(TAG);
    }

    @覆盖
    保护无效onHandleIntent(意向意图){
        Log.i(udazzT,设置标记);
        共享preferences共享preferences = preferenceManager.getDefaultShared preferences(本);

        尝试 {
            //在同时出现多个刷新操作的(不太可能)的事件,
            //确保它们被顺序处理。
            同步(TAG){
                // [开始register_for_gcm]
                //最初这个调用出去的网络检索令牌,后续调用
                //是本地的。
                // [开始的get_token]
                Log.i(udazzT,设置令牌2);
                实例id =实例ID InstanceID.getInstance(本);
                字符串标记= instanceID.getToken(的getString(R.string.gcm_defaultSenderId)
                        GoogleCloudMessaging.INSTANCE_ID_SCOPE,NULL);
                // [结束的get_token]
                Log.i(TAG,GCM注​​册令牌:+令牌);

                // TODO:实现此方法发送的任何注册到你的应用程序的服务器。
                sendRegistrationToServer(标记);

                //订阅话题渠道
                subscribeTopics(标记);

                //你应该存储一个布尔值,指示生成的令牌是否已经
                //发送到您的服务器。如果布尔是假的,送令牌服务器,
                //否则,你的服务器应该已经收到该邮件。
                共享preferences.edit()putBoolean(快速入门preferences.SENT_TOKEN_TO_SERVER,真)。适用()。
                // [结束register_for_gcm]
            }
        }赶上(例外五){
            Log.d(TAG,完成令牌刷新失败,E);
            //如果在获取新令牌或更新我们的注册数据发生异常
            //第三方的服务器上,这样可以保证我们会尝试更新在稍后的时间。
            共享preferences.edit()putBoolean(快速入门preferences.SENT_TOKEN_TO_SERVER,假)。适用()。
        }
        //通知用户界面,注册已经完成,所以进度指示器可以隐藏。
        意图registrationComplete =新的意向书(快速入门preferences.REGISTRATION_COMPLETE);
        LocalBroadcastManager.getInstance(本).sendBroadcast(registrationComplete);
    }

    / **
     *坚持登记的第三方服务器。
     *
     *修改该方法,以该用户的GCM登记令牌与任何服务器端帐户相关联
     *您的应用程序维护。
     *
     *参数令牌新的令牌。
     * /
    私人无效sendRegistrationToServer(字符串标记){
        //添加自定义的实现,根据需要。
    }

    / **
     *订阅感兴趣的任何话题GCM由话题不断的定义。
     *
     *参数令牌GCM令牌
     * @throws IOException异常,如果无法达到GCM PubSub的服务
     * /
    // [开始subscribe_topics]
    私人无效subscribeTopics(字符串标记)抛出IOException异常{
        对于(字符串主题:主题){
            GcmPubSub PubSub的= GcmPubSub.getInstance(本);
            pubSub.subscribe(令牌,/主题/+主题,NULL);
        }
    }
    // [结束subscribe_topics]

}
 

解决方案

请确保它是注册在你的清单。

 <服务机器人:名称=。RegistrationIntentService
             机器人:出口=FALSE/>
 

I call RegistrationIntentService:

    if (checkPlayServices()) {
        Log.i("udazzT", "check servicies");
        // Start IntentService to register this application with GCM.
        Intent intent2 = new Intent(this, RegistrationIntentService.class);
        startService(intent2);
    }

But I cannot see any of the logs in RegistrationIntentService:

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";
    private static final String[] TOPICS = {"global"};

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("udazzT", "setting token");
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        try {
            // In the (unlikely) event that multiple refresh operations occur simultaneously,
            // ensure that they are processed sequentially.
            synchronized (TAG) {
                // [START register_for_gcm]
                // Initially this call goes out to the network to retrieve the token, subsequent calls
                // are local.
                // [START get_token]
                Log.i("udazzT", "setting token 2");
                InstanceID instanceID = InstanceID.getInstance(this);
                String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                        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) {
        // Add custom implementation, as needed.
    }

    /**
     * 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);
        }
    }
    // [END subscribe_topics]

}

解决方案

Make sure that it is registered in your manifest.

<service android:name=".RegistrationIntentService"
             android:exported="false" />

这篇关于Android的 - 没有得到GCM令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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