15

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

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" />

在应用程序标签中:

    <receiver android:name="com.parse.ParseBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
            <action android:name="act" />
        </intent-filter>
    </receiver>

    <receiver android:name="com.app.example.PushReceiver" >

        <intent-filter>
            <action android:name="act" />
            </action>
        </intent-filter>
    </receiver>

    <receiver
        android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <action android:name="act" />
            <category android:name="com.example.app" />
        </intent-filter>
    </receiver>

每次我安装时,它都会显示如下所示的错误。

03-10 12:18:48.555: E/ParseCommandCache(12709): Failed to run command.
03-10 12:18:48.555: E/ParseCommandCache(12709): com.parse.ParseException: at least one ID field (installationId,deviceToken) must be specified in this operation
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.ParseCommand$3.then(ParseCommand.java:348)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$10.run(Task.java:452)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$1.execute(Task.java:68)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.completeImmediately(Task.java:448)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.continueWith(Task.java:322)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.continueWith(Task.java:333)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$8.then(Task.java:385)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$8.then(Task.java:1)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$11.run(Task.java:485)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$1.execute(Task.java:68)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.completeAfterTask(Task.java:481)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.access$9(Task.java:477)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$7.then(Task.java:350)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$7.then(Task.java:1)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.runContinuations(Task.java:514)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.access$5(Task.java:510)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at   com.parse.Task$TaskCompletionSource.trySetResult(Task.java:569)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$TaskCompletionSource.setResult(Task.java:603)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$11$1.then(Task.java:497)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$11$1.then(Task.java:1)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$10.run(Task.java:452)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$1.execute(Task.java:68)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.completeImmediately(Task.java:448)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.access$8(Task.java:444)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$6.then(Task.java:315)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$6.then(Task.java:1)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.runContinuations(Task.java:514)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.access$5(Task.java:510)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:569)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$TaskCompletionSource.setResult(Task.java:603)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$11$1.then(Task.java:497)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$11$1.then(Task.java:1)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$10.run(Task.java:452)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$1.execute(Task.java:68)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.completeImmediately(Task.java:448)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.continueWith(Task.java:322)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.continueWith(Task.java:333)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$11.run(Task.java:489)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$1.execute(Task.java:68)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.completeAfterTask(Task.java:481)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.access$9(Task.java:477)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$7.then(Task.java:350)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$7.then(Task.java:1)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.runContinuations(Task.java:514)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task.access$5(Task.java:510)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$TaskCompletionSource.trySetResult(Task.java:569)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$TaskCompletionSource.setResult(Task.java:603)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at com.parse.Task$3.run(Task.java:228)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-10 12:18:48.555: E/ParseCommandCache(12709):     at java.lang.Thread.run(Thread.java:841)

我只想在设备上注册一次。如果在设备中多次安装应用程序应该没有问题。请帮助我,提前谢谢。

在我的情况下,三星标签 3 使用相同的 UniqueId 注册了两次。UniqueId 在解析数据库中被标记为唯一列吗?或者我们应该让它独一无二?如果是,如何?

4

7 回答 7

8

我在使用 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();

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

于 2014-03-19T07:58:16.503 回答
8

我认为 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();
});
});

请注意,所有者是您认为可以使用的用户名或主键。

这是相同的链接,mukul 有更好的解释 https://www.parse.com/questions/check-for-duplicate-installations-of-same-user-on-re-installation-of-app

于 2014-10-31T12:00:12.667 回答
7

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

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

context - 这用于访问本地存储以缓存订阅,因此它当前必须是可行的上下文。

(从这里引用)。

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

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

于 2014-03-10T12:41:00.103 回答
1

我也面临这个问题。我通过在我的活动中调用以下方法来解决它onCreate()

/**
     * Initialize Push Messaging Service and subscribe to all-users channel
     */
    private void initParsePushMessaging() {
        ParseInstallation parseInstallation = ParseInstallation
                .getCurrentInstallation();
        //You might skip this if
        if (ParseUser.getCurrentUser() != null) {
            parseInstallation.put("user",
                    ParseUser.getCurrentUser());
        }
        if (parseInstallation.getObjectId() != null)
            parseInstallation.saveInBackground(new SaveCallback() {

                @Override
                public void done(ParseException e) {
                    PushService.subscribe(getApplicationContext(),"channel_name",
                            MainHomeActivity.class);
                }
            });

    }

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

于 2014-09-08T12:42:15.870 回答
1

对我来说摆脱这个异常的方法是使用saveEventually()而不是saveInBackground().

在这里,您可以找到我对类似问题的回答的链接。

我认为这saveEventually()是一个更好的选择,因为它可以确保始终保存安装,而不管网络可用性如何。相反,saveInBackground()由于没有网络连接,保存可能会失败。saveEventually()你也不需要做任何错误检查,你应该在with中SaveCallback()saveInBackground()

关于重复通知,如果您使用的是最新的 Parse SDK,则不应该发生这种情况(1.7.1 不会发生在我身上)。有一个bug,现在已经解决了。请参阅这个 SO question这个 FB bug

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

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

于 2014-12-02T22:05:48.957 回答
1

我找到了解决方法,它将再次在 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();
    }

而在初始化 Parse 时

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();
                }
            }
        });
于 2015-07-08T13:07:24.653 回答
0

创建新类并使用 Application 扩展它。这样写代码

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

}

manifest在文件中添加条目

<application android:label="@string/app_name"
    android:name="org.cocos2dx.cpp.BBApplication"
             android:icon="@drawable/icon">
于 2015-07-06T07:47:25.313 回答