gpt4 book ai didi

android - 每次应用程序启动时请求 Google Cloud Messaging (GCM) 注册 ID

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:56 24 4
gpt4 key购买 nike

我读过有关 GCM 可能不定期刷新注册 ID 的文章。我正在尝试使用推送通知构建应用程序,但不确定如何处理此类刷新的注册 ID。

我的第一个策略是每次应用程序启动时请求注册 ID 并将其发送到应用程序服务器。它看起来工作正常但听起来有点不对...

这样可以吗?

最佳答案

基本上,您应该在主要 Activity 中执行以下操作:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);

final String regId = GCMRegistrar.getRegistrationId(this);

if (regId.equals("")) {
GCMRegistrar.register(this, GCMIntentService.GCM_SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
}

之后,您应该将注册 ID 发送到您的应用程序服务器,每当应用程序收到一个 com.google.android.c2dm.intent.REGISTRATION Intent 和一个 registration_id 额外。当 Google 定期更新应用的 ID 时,可能会发生这种情况。

您可以通过使用您自己的实现扩展 com.google.android.gcm.GCMBaseIntentService 来实现此目的,例如:

public class GCMIntentService extends GCMBaseIntentService {

// Also known as the "project id".
public static final String GCM_SENDER_ID = "XXXXXXXXXXXXX";

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
super(GCM_SENDER_ID);
}

@Override
protected void onRegistered(Context context, String regId) {
// Send the regId to your server.
}

@Override
protected void onUnregistered(Context context, String regId) {
// Unregister the regId at your server.
}

@Override
protected void onMessage(Context context, Intent msg) {
// Handle the message.
}

@Override
protected void onError(Context context, String errorId) {
// Handle the error.
}
}

有关更多详细信息,我会(重新)阅读 writing the client side code 的文档和 the Advanced Section of the GCM documentation .

希望对您有所帮助!

关于android - 每次应用程序启动时请求 Google Cloud Messaging (GCM) 注册 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13300527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com