我正在尝试使用GCM for android。在developer.google提供的一些步骤之后,我在toast中获得了注册ID,现在我想将它存储在数据库中。如何更改此处以将其存储在数据库中。我需要为这次更新改变哪些步骤???
码
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
String token = intent.getStringExtra("token");
Toast.makeText(getApplicationContext(), "GCM registration token: " + token, Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(config.SENT_TOKEN_TO_SERVER)) {
// gcm registration id is stored in our server's MySQL
Toast.makeText(getApplicationContext(), "GCM registration token is stored in server!", Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(config.PUSH_NOTIFICATION)) {
// new push notification is received
Toast.makeText(getApplicationContext(), "Push notification is received!", Toast.LENGTH_LONG).show();
}
}
};
答案 0 :(得分:1)
您可以将GCM令牌存储在SharedPreferences中。
以下是您收到令牌后可以添加的一小段代码:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor e = sharedPreferences.edit();
e.putString("GCMTOKEN", token);
e.commit();
要获得令牌,你可以使用它,
String token = sharedPreferences.getString("GCMTOKEN", null);
答案 1 :(得分:0)
我建议您将令牌保留为SharedPreference中的键值对。您可以轻松地使用它来检索和保存/更新您的注册ID,而不是在mySQL中创建记录。
在这个问题的最佳答案中有一个实现。看看吧。
答案 2 :(得分:0)
您可以按照此示例了解如何处理推送令牌注册和刷新令牌,这是一个重要的部分