Android解析推送通知设备仅在一台设备上注册一次

Android解析推送通知设备仅在一台设备上注册一次,android,push-notification,parse-platform,google-cloud-messaging,devicetoken,Android,Push Notification,Parse Platform,Google Cloud Messaging,Devicetoken,我正在使用解析服务在我的应用程序中发送推送通知。但当我在一台设备上重新安装应用程序时,它会一直注册。问题是,一台设备在每个设备上都会收到多个通知。我已经为注册做了一些代码,如下所示。请帮助我,提前谢谢 Parse.initialize(this, PARSE_APP_ID, PARSE_CLIENT_KEY); ParseACL defaultACL = new ParseACL(); defaultACL.setPublicReadAccess(true); ParseACL.setDefau

我正在使用解析服务在我的应用程序中发送推送通知。但当我在一台设备上重新安装应用程序时,它会一直注册。问题是,一台设备在每个设备上都会收到多个通知。我已经为注册做了一些代码,如下所示。请帮助我,提前谢谢

Parse.initialize(this, PARSE_APP_ID, PARSE_CLIENT_KEY);
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().getInstallationId();
ParseInstallation.getCurrentInstallation().saveInBackground();
及订阅:

PushService.subscribe(this, userName, Detail.class);
明显地

在上面

  <permission
    android:name="com.example.app.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

  <uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />
我只想在设备上注册一次。如果应用程序在设备中多次安装,则不会出现问题。请帮助我,提前感谢

Parse.initialize(this, PARSE_APP_ID, PARSE_CLIENT_KEY);
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().getInstallationId();
ParseInstallation.getCurrentInstallation().saveInBackground();

在我的例子中,三星tab 3使用相同的UniqueId注册了两次。UniqueId在parse db中是否标记为唯一列?或者我们应该让它独一无二?如果是,如何进行?

PushService.subscribe
似乎将订阅缓存在本地存储中,以避免在多次启动应用程序时重新订阅

这是该方法的第一个参数的用途:

上下文-用于访问本地存储以缓存 订阅,因此它当前必须 成为一个可行的环境

(引自)

但是,当您卸载应用程序时,该应用程序的本地存储将从您的设备中删除,因此新安装将导致
PushService.subscribe
重新注册到Google云消息。如果新注册返回一个新的注册ID,Parse将有两个注册ID,可用于向应用程序发送推送通知,并且这两个ID都将链接到您提供给
subscribe
的相同用户名。因此,向该用户名发送通知会将其发送到两个注册ID,导致其到达两次


当Parse为您发送通知时,他们应该从Google获得一个带有
canonical_registration_id
的响应,这将让他们知道与您设备上的应用程序关联的一个注册id是旧的,不应该再使用。因此(假设Parse有一个不错的GCM实现)下次向设备发送通知时,您应该只收到一次

我在用android设备的唯一id发送更新表后得到了它

 String  android_id = Secure.getString(getApplicationContext().getContentResolver(),Secure.ANDROID_ID);         
    Log.e("LOG","android id >>" + android_id);

    PushService.setDefaultPushCallback(this, MainActivity.class);

    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("installationId",android_id);

    installation.saveInBackground();

它将更新原始数据,但不会重新注册设备。

我也面临这个问题。我通过在我的活动的
onCreate()


虽然这并没有完全解决我的问题,但现在我的应用程序并没有挂起,也没有更多的ANR是由于这个解析实现。如果我重新安装一个应用程序并立即运行,那么该应用程序将创建一个新的安装记录,并删除最后一个记录。唯一的问题是,频道名称在本次运行中未订阅,但在下一次运行中,频道成功订阅

我认为Mukul为这个问题提供了很好的云代码

给你

Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.Installation);
query.equalTo("owner", request.user);
query.equalTo("uniqueID", request.object.get("uniqueID"));
query.first().then(function(duplicate) {
    if (typeof duplicate === "undefined") {
        console.log("Duplicate does not exist,New installation");
        response.success();
    } else {
        console.log("Duplicate exist..Trying to delete " + duplicate.id);
        duplicate.destroy().then(function(duplicate) {
            console.log("Successfully deleted duplicate");
            response.success();
        }, function() {
            console.log(error.code + " " + error.message);
            response.success();
        });

    }
}, function(error) {
    console.warn(error.code + error.message);
    response.success();
});
});
注意,owner是您认为您可以使用的用户名或主键

这是mukul的更好解释的链接
对我来说,解决这个异常的有效方法是使用
savefinally()
而不是
saveInBackground()

这是我对一个类似问题的回答

我认为
savefinally()。相反,如果使用
saveInBackground()
,则有可能由于没有网络连接而导致保存失败。另外,使用
savefinally()
时,您不需要执行任何错误检查,而应该使用
saveInBackground()
SaveCallback()中执行此操作

关于重复通知,如果您使用的是最新的Parse SDK,则不会发生这种情况(我在1.7.1中不会发生这种情况)。有一个bug现在已经解决了。见和

请注意,用户在重新安装应用程序后第一次收到通知时,此通知可以发送两次。这发生在我身上,但只发生在第一次通知时。(有关更多详细信息,请参阅FB链接。)在重复通知之后,解析将自动删除旧安装。这是我的经验


如果您试图通过在CloudCode中实现一些逻辑来避免发送重复通知(使用保存新安装时触发的beforeSave,检查应用程序是否已安装到设备中并删除旧安装),请不要这样做!没有必要。Parse将为您执行此操作:它将删除旧安装:)

创建新类并使用应用程序扩展它。 以这种方式编写代码

  public class BBApplication extends Application {


   @Override
   public void onCreate() {
      super.onCreate();

       Parse.initialize(this, "app key", "client key");
          ParseInstallation.getCurrentInstallation().saveInBackground();
           ParsePush.subscribeInBackground("", new SaveCallback() {

               @Override
               public void done(com.parse.ParseException arg0) {
                // TODO Auto-generated method stub
                     if (arg0 == null) {
                         Log.d("com.parse.push", "successfully    subscribed to the broadcast channel.");
                    }    else {
                          Log.e("com.parse.push", "failed to subscribe for push", arg0);
                    }
            }
            });

       ParseUser.enableAutomaticUser();
       ParseACL defaultACL = new ParseACL();
      // Optionally enable public read access.
      // defaultACL.setPublicReadAccess(true);
      ParseACL.setDefaultACL(defaultACL, true);
}
}

清单
文件中添加条目

<application android:label="@string/app_name"
    android:name="org.cocos2dx.cpp.BBApplication"
             android:icon="@drawable/icon">

我找到了解决办法,可以在Parse中重新注册已删除的安装

public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (s.equals("app_Parse")) {
                    deleteDir(new File(appDir, s));
                    System.out.println( "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }

我有同样的问题,我得到这个错误,我没有注册“objectID”的通道。你知道有什么解决办法吗?它是用相同的UniquedInstallationId创建新行,它是一个只读字段。更新该字段的唯一方法是使用CloudCode并使用主密钥。这对我也不起作用:获取错误:“无法修改\u安装对象的
installationId
属性”。这样,重复的对象被删除,但我无法获取它的通道??如何保持该安装的通道?@PalakDarji这完全取决于您使用的结构。我要做的是在用户类中保留安装对象引用,并且每当在删除重复Id之前创建新的安装Id时,我都会将所有用户保存的数据从重复的安装obj复制到当前的安装obj。。i、 e.除了替换自动创建的解析令牌、Android、安装id。。。。。。我用重复的条目替换它。。希望你明白我想表达的意思1.很整洁,但我有
<application android:label="@string/app_name"
    android:name="org.cocos2dx.cpp.BBApplication"
             android:icon="@drawable/icon">
public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (s.equals("app_Parse")) {
                    deleteDir(new File(appDir, s));
                    System.out.println( "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }
Parse.initialize(this, Constants.PARSE_APP_ID, Constants.PARSE_CLIENT_ID);

        ParseInstallation installation = ParseInstallation.getCurrentInstallation();

//Trying  to update the current installation with a custom key and this will trigger the ParseException if this installation is not found in Installations Table in Parse Server
        String value= "Value";

        if(installation.get("customKey") != null){
            value= installation.get("customKey").toString();
        }
        installation.put("customKey", value);
//Now lets see what call back brings in
        installation.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                System.out.println("Done");
                if (e == null) {
                    System.out.println("Succesfull Registration.....");
                } else {

                    System.out.println("Cleare cache");

//By clearing the cache the next time user will close and re open the app it will be installed in installations again
                    clearApplicationData();
                }
            }
        });