gpt4 book ai didi

android - 谷歌云消息 : duplicate token in database

转载 作者:行者123 更新时间:2023-11-30 03:12:31 24 4
gpt4 key购买 nike

我使用 Google Cloud Messaging 发送通知。我的应用程序将 token (显然是从 Google 提供的)发送到我的服务器数据库...问题是 save token request 同时被发送到服务器两次(完全相同,像一个克隆人)!所以,通过服务器我无法解决这个问题(因为时间是相同的,我不能告诉 insert if not exist)...通过应用程序我不知道我是否有问题。

我认为问题出在 Google,但我不知道。

这是我的注册码:

        GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
new registra_background().execute();
} else {
if (!GCMRegistrar.isRegisteredOnServer(this)) {
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
ServerUtilities.register(context, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}

这是被调用的类

private class registra_background extends AsyncTask<Void, Integer, Void> {
int progress_status;
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params){
try{
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regId = gcm.register(SENDER_ID);
Log.e("TOKEN",regId);
}catch(Exception ex){
System.out.println("Errore dati");
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values){
}
@Override
protected void onPostExecute(Void result){
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
ServerUtilities.register(context, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}

这是服务器类

public final class ServerUtilities {
private static final int MAX_ATTEMPTS = 5;
private static final int BACKOFF_MILLI_SECONDS = 2000;
private static final Random random = new Random();
static public void register(final Context context, final String regId) {
Database db;
db = new Database(context);
try {
db.open();
} catch (SQLException sqle) {
throw sqle;
}
Cursor variabili=db.variabili();
int x=1;
String pvPref="",pvPrefProv="",pvPrefCitta="",pvPrefIndirizzo="";
while(variabili.moveToNext()){
if(x==1){
pvPref=variabili.getString(variabili.getColumnIndex("valore"));
}else if(x==2){
pvPrefProv=variabili.getString(variabili.getColumnIndex("valore"));
}else if(x==3){
pvPrefCitta=variabili.getString(variabili.getColumnIndex("valore"));
}else if(x==4){
pvPrefIndirizzo=variabili.getString(variabili.getColumnIndex("valore"));
}
x++;
}
String serverUrl = SERVER_URL;
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("token_push", regId);
params.put("pvPref", pvPref);
params.put("device",android.os.Build.MODEL);
params.put("pvPrefProv", pvPrefProv);
params.put("pvPrefCitta", pvPrefCitta);
params.put("pvPrefIndirizzo", pvPrefIndirizzo);
params.put("app_version", "2.0");
params.put("os", Build.VERSION.RELEASE);
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
try {
post(serverUrl, params);
SystemClock.sleep(500);
GCMRegistrar.setRegisteredOnServer(context, true);
return;
} catch (IOException e) {
if (i == MAX_ATTEMPTS) {
break;
}
try {
Thread.sleep(backoff);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
return;
}
backoff *= 2;
}
}
}
public static void unregister(final Context context, final String regId) {
String serverUrl = SERVER_URL + "/unregister";
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
try {
post(serverUrl, params);
GCMRegistrar.setRegisteredOnServer(context, false);
} catch (IOException e) {
}
}
private static void post(String endpoint, Map<String, String> params) throws IOException {
URL url;
try {
url = new URL(endpoint);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + endpoint);
}
StringBuilder bodyBuilder = new StringBuilder();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
int status = conn.getResponseCode();
if (status != 200) {
throw new IOException("Errore " + status);
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
}

最佳答案

您的代码将新的注册方法 (GoogleCloudMessaging.register) 与旧的已弃用的 GCMRegistrar 类混合在一起。而且两个Async Tasks的嵌套用法很困惑。

如果您在服务器上收到两个具有相同注册 ID 的请求,那么您的客户端代码是唯一的负责人。与其尝试调试这些过于复杂的代码,我建议您使用当前的 Google GCM Demo作为引用。

关于android - 谷歌云消息 : duplicate token in database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703136/

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