每个人都在我的应用中使用解析服务进行推送通知。但是当我在一台设备上重新安装应用程序时,它会一直注册。然后问题是,一台设备会在每台设备上收到多个通知。我已经完成了一些注册代码,如下所示。请帮助我,提前谢谢。
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是否在解析db中标记为唯一列?或者我们应该让它独一无二?如果有,怎么样?
答案 0 :(得分: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();
它将更新raw,但它不会重新注册设备。
答案 1 :(得分: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();
});
});
请注意,owner是您认为可以使用的用户名或主键。
这是与mukul更好解释相同的链接 https://www.parse.com/questions/check-for-duplicate-installations-of-same-user-on-re-installation-of-app
答案 2 :(得分:7)
PushService.subscribe
似乎将订阅缓存在本地存储中,以避免在多次启动应用时重新订阅。
这是该方法的第一个参数用于:
context - 用于访问本地存储以缓存 订阅,所以它当前必须 是一个可行的背景。
(引自here)。
但是,当您卸载该应用时,该应用的本地存储将从您的设备中删除,因此新安装将导致PushService.subscribe
重新注册到Google Cloud Messaging。如果新注册返回新的注册ID,则Parse将有两个注册ID,可用于向您的应用发送推送通知,并且它们都将链接到您提供给subscribe
的同一用户名。因此,向该userName发送通知会将其发送到两个注册ID,导致它到达两次。
当Parse为您发送通知时,他们应该从Google获得canonical_registration_id
的回复,这会让他们知道您设备上与您的应用相关联的注册ID之一已过时,不应使用了。因此(假设Parse有一个不错的GCM实现),下次您向设备发送通知时,您应该只收到一次。
答案 3 :(得分: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 未在此次投放中订阅,但会在下次投放时成功订阅。
答案 4 :(得分:1)
使用saveEventually()
代替saveInBackground()
, 除了saveEventually()
之外,对我来说有什么用呢?
这里有a link我对类似问题的回答。
我认为saveInBackground()
是更好的选择,因为无论网络可用性如何,它都能确保始终保存安装。相反,对于saveEventually()
,由于没有网络连接,保存可能会失败。另外,对于SaveCallback()
,您无需执行任何错误检查,您应该在saveInBackground()
{{1}}进行错误检查。
关于重复通知,如果您使用的是最新的Parse SDK,则不会发生这种情况(1.7.1版本不会发生这种情况)。现在已经解决了一个错误。请参阅this SO question和this FB bug。
请注意,用户首次在重新安装应用后收到通知,此通知可以两次发送。它发生在我身上,但只发生在第一次通知中。 (有关详细信息,请参阅FB链接。)在重复通知之后,Parse将自动删除旧安装。这是我的经历。
如果您尝试通过在CloudCode上实施某些逻辑来避免发送重复通知(使用在保存新安装时触发的beforeSave,请检查该应用是否已安装到设备中并删除旧安装),< em>不要那样做!没有必要。 Parse将为您完成:它将删除旧安装:)
答案 5 :(得分: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();
}
}
});
答案 6 :(得分: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">