Android的GCM集成

4

尽管我按照Google开发者页面上的所有指示操作了Gcm(清单设置、配置文件、在控制台中注册等),但仍然存在问题。

 public class MyInstanceIDListenerService extends IntentService {
private static final String TAG = "RegIntentService";
private static final String[] TOPICS = {"global"};

public MyInstanceIDListenerService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    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(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]
和无法解决。为什么?我该怎么办?如何解决这个问题?
1个回答

1
"

gcm_defaultSenderId 是您必须创建的字符串,它是您的应用程序发送者 ID。

QuickStartPreferences 是一个示例。如果您想记住令牌已发送到服务器(请参见上一行的注释),则可以创建一个类来保存首选项键。

"

快速启动设置的首选项,我只是想确保一下,配置文件的目标之一是创建gcm_defaultSenderId,对吗? - Bad0
啊,是的,也许吧。我没有使用它,我使用Java常量来存储我的发件人ID。你在你的gradle文件中启用了插件吗? - clemp6r
是的,我已经启用了它,并且在几个项目中进行了多次配置。我知道一定是我漏掉了什么,但我找不到它。 - Bad0
1
如果您将google-services.json文件放在正确的位置(app/google-services.json),并确保gradle版本、依赖和插件正确,R.string.gcm_defaultSenderId将会被Gradle自动创建。 - SalmanShariati

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