Как сделать недействительным токен для C2DM с предыдущими установками? - PullRequest
0 голосов
/ 29 сентября 2011

Поскольку, по-видимому, нет надежного способа получить уникальный идентификатор устройства, наше приложение отслеживает установку с использованием этого класса ...

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) {  
            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();
    }
}

Полученный UUID отправляется обратно на наш сервер с токеном C2DM. Мы отправляем C2DM-уведомления на устройство на основе этого токена. Однако, если приложение будет удалено, мы не сможем узнать об этом, и любые последующие установки не смогут аннулировать токен, и Google, похоже, не сделает это автоматически. Это приводит к тому, что устройства, которые были установлены более одного раза, получают двойные уведомления.

Есть идеи?

1 Ответ

0 голосов
/ 30 сентября 2011

Можете ли вы отменить регистрацию приложения в C2DM по:

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);
}
...