安卓设备无法获取GCM令牌

8

我调用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);
    }

但我无法在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]

}

你有没有阅读过这个问题:https://dev59.com/HWgt5IYBdhLWcg3w7BuL? - Andrea Thacker
该示例中的register方法已被弃用。 - user3109249
4个回答

16

确保它在您的清单文件中注册。

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

@MarianoPelizzari 你是否看到了意图之前的日志输出?Log.i("udazzT", "check servicies"); - mattfred
是的,我看到那个了。 - user3109249
尝试使用以下内容包含一个意图过滤器:<action android:name="com.google.android.c2dm.intent.RECEIVE" /> - Koh
我开始了一个新的项目,并且问题使用 <service android:name=".RegistrationIntentService" android:exported="false" /> 得到了修复。 - user3109249
2
在我的情况下,我将RegistrationIntentService放在一个名为.gcm的包中,因此Android Studio中的IDE就像这样制作它:<service android:name=".gcm.RegistrationIntentService"... /> 这就是为什么在我的情况下它没有起作用。我所要做的就是对其进行完整引用:<service android:name="com.mycompany.gcm.RegistrationIntentService"... /> 现在它可以正常工作了。 - Aegir

2

你是否考虑过像 googlesamples 这样添加所有必要的内容?


我做了,并且我多次进行了审查。 - user3109249
好的,因为我忘记了一些方法,但现在我用googlesamples可以工作了。 - Emmanuel Loisance

1
只需添加此内容。
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<PACKAGE-NAME>" />
</intent-filter>

在我的代码中,“GcmReceiver”和“MyGcmListenerService”的清单文件(manifest.xml)如下所示:

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="<PACKAGE-NAME>" />
        </intent-filter>
    </receiver>
    <!-- [END gcm_receiver] -->

    <!-- [START gcm_listener] -->
    <service
        android:name="<PACKAGE-NAME>.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="<PACKAGE-NAME>" />
        </intent-filter>
    </service>
    <!-- [END gcm_listener] -->
    <!-- [START instanceId_listener] -->

在我的情况下它是有效的...也许对你有帮助。祝你好运。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接