android - 适用于 Android 的 GCM 集成

标签 android google-cloud-messaging

即使我完成了 Gcm 的 Google 开发者页面中的所有说明( list 设置、配置文件、在控制台中注册......)

 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]

gcm_defaultSenderId 和 QuickStartPreferences 无法解析。为什么 ??我怎样才能克服它??

最佳答案

gcm_defaultSenderId 是您必须创建的字符串。这是您的应用发件人 ID。

QuickStartPreferences 作为示例。如果您想记住 token 已发送到您的服务器(请参阅此行上方的注释),您可以创建一个类来保存首选项键。

关于android - 适用于 Android 的 GCM 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393014/

相关文章:

Android:可绘制完美的圆角

android - 如何将 GCM 推送通知消息存储到 sqlite 数据库中?

java - 检查应用程序是否在 GCM onMessage 事件期间打开?

android - GCM 优先级消息不会唤醒我的应用程序

android - 从 GCMIntentService 发送数据并在主要 Activity 中接收它

java - 与多个 fragment 通信的 Android 服务

java - OnPost执行结果为空

java - 使用后台服务播放音乐时,音乐在移动到另一个 Activity 时停止

android - 在 Android 上使用 GData API 的最佳选择?

android - 在库中实现 Firebase