How to invalidate token for C2DM with previous installs?
Since there is apparently no reliable way to get the devices unique identifier, our app tracks the installation using this class...
package com.themenetwork.app.misc;
import java.io.*;
import java.util.UUID;
import android.content.Context;
public class Installation {
private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
public synchronized static String id(Context context) {
if (sID == null开发者_StackOverflow) {
File installation = new File(context.getFilesDir(), INSTALLATION);
try {
if (!installation.exists())
writeInstallationFile(installation);
sID = readInstallationFile(installation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return sID;
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
f.close();
return new String(bytes);
}
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation);
String id = UUID.randomUUID().toString();
out.write(id.getBytes());
out.close();
}
}
The resulting UUID is sent back to our server, with the C2DM token. We send C2DM notifications to the device based on this token. However, if the app is removed we have no way of knowing and any subsequent installs have no way of invalidating the token and Google doesn't appear to be doing it automatically. This is causing devices who have installed more than once to get duplicate notifications.
Any ideas?
Can you unregister the App from C2DM by:
public static final String REQUEST_UNREGISTRATION_INTENT = "com.google.android.c2dm.intent.UNREGISTER";
public static final String GSF_PACKAGE = "com.google.android.gsf";
public static final String EXTRA_APPLICATION_PENDING_INTENT = "app";
public static void unregister(Context context) {
Intent regIntent = new Intent(REQUEST_UNREGISTRATION_INTENT);
regIntent.setPackage(GSF_PACKAGE);
regIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT, PendingIntent.getBroadcast(context, 0, new Intent(), 0));
context.startService(regIntent);
}
精彩评论