目前,我正在使用Amazon SNS管理Google GCM,即推送通知。
问题是,我只能在创建这样的结束点后获得端点ARN,
但是如何检查用户之前是否已注册?所以我不需要再创建一个记录
并且只使用那个旧记录。感谢
以下代码是用户在亚马逊开启(注册)/关闭(删除)SNS的按钮
notify.setOnClickListener(new OnClickListener() {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void onClick(View v) {
if (prefs.getString("endpoint_arn", "").equals("")) {
new AsyncTask() {
@Override
protected Object doInBackground(final Object... params) {
try {
((Activity) ctx).runOnUiThread(new Runnable() {
public void run() {
if (!pDialog.isShowing())
pDialog.show();
}
});
String token = gcm.register(Constant.projectID);
CreatePlatformEndpointRequest per = new CreatePlatformEndpointRequest();
per.setToken(token);
per.setPlatformApplicationArn(Constant.platformARN);
CreatePlatformEndpointResult result = asnsc.createPlatformEndpoint(per);
editor.putString("endpoint_arn", result.getEndpointArn());
editor.commit();
((Activity) ctx).runOnUiThread(new Runnable() {
public void run() {
notify_txt.setText(ctx.getResources().getString(R.string.on));
if (pDialog.isShowing())
pDialog.dismiss();
}
});
} catch (final Exception e) {
((Activity) ctx).runOnUiThread(new Runnable() {
public void run() {
Log.d("test1","Registration Error:" + e.getMessage());
Toast.makeText(ctx, getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
if (pDialog.isShowing())
pDialog.dismiss();
}
});
}
return true;
}
}.execute(null, null, null);
} else {
new AsyncTask() {
@Override
protected Object doInBackground(final Object... params) {
try {
((Activity) ctx).runOnUiThread(new Runnable() {
public void run() {
if (!pDialog.isShowing())
pDialog.show();
}
});
DeleteEndpointRequest dPer = new DeleteEndpointRequest();
dPer.setEndpointArn(prefs.getString("endpoint_arn", ""));
asnsc.deleteEndpoint(dPer);
gcm.unregister();
editor.remove("endpoint_arn").commit();
((Activity) ctx).runOnUiThread(new Runnable() {
public void run() {
notify_txt.setText(ctx.getResources().getString(R.string.off));
if (pDialog.isShowing())
pDialog.dismiss();
}
});
} catch (final Exception e) {
((Activity) ctx).runOnUiThread(new Runnable() {
public void run() {
Log.d("test1","Delete Error:" + e.getMessage());
Toast.makeText(ctx, getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
if (pDialog.isShowing())
pDialog.dismiss();
}
});
}
return true;
}
}.execute(null, null, null);
}
}
});
答案 0 :(得分:3)
据我所知,即使用户在同一设备上重新安装应用程序,GCM生成的设备令牌也是一样的。如果您已经使用令牌注册了设备,则SNS不会插入另一条记录,而是指相同的记录。因此,无需担心使用设备令牌再次向SNS注册用户。