Every one I am using the parse service for push notification in my app. but it register all time when i re-install the app in one device.Then problem is that,one device rece
What worked for me to get rid of this exception was using saveEventually()
instead of saveInBackground()
.
Here you have a link to my answer to a similar question.
I think that saveEventually()
is a better option because it assures that the installation will always be saved, regardless of the netwwork availability. In contrast, with saveInBackground()
there is a chance that the save fails due to no network connectivity. Also with saveEventually()
you don't need to do any error checking, which you should do in a SaveCallback()
with saveInBackground()
.
Regarding the duplicate notifications, this shouldn't occur if you are using the latest Parse SDK (it doesn't happen to me with 1.7.1). There was a bug that has been solved now. See this SO question and this FB bug.
Note that first time the user receives a notification after reinstalling the app, this notification can be delivered twice. It has happened to me, but only happens for the very first notification. (See the FB link for more details.) After that duplicate notification, the old installation will be removed automatically by Parse. This is my experience.
If you are trying to avoid sending duplicate notifications by implementing some logic at the CloudCode (using a beforeSave that triggers when saving a new Installation, check if the app had already been installed into the device and deleting the old installation), don't do that! There is not need. Parse will do it for you: it will delete the old installation :)
I think Mukul has provided great cloud code for this issue
here it is
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();
});
});
Note that owner is the username or the primary key that you think u can use.
here's the link of the same with better explanation by mukul https://www.parse.com/questions/check-for-duplicate-installations-of-same-user-on-re-installation-of-app
I was also facing this issue. I sort of solved it by calling the below method in my Activity's 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);
}
});
}
Even though it didn't completely solve my problem but now my app doesn't hang and no more ANR's due to this Parse implementation. If i re install an app and run it now then the app creates a new installation record and remove's the last one. The only problem is that the channel_name is not subscribed on this run but on the next run the channel are successfully subscribed.
I got it after update the table with the send of unique id of the android device.
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();
It will update the raw ,but it doesn't re-register the device .
PushService.subscribe
seems to cache the subscription in local storage, to avoid re-subscribing when you launch the app multiple times.
This is what the first parameter of that method is used for :
context - This is used to access local storage to cache the subscription, so it must currently be a viable context.
(quote from here).
However, when you uninstall the app, local storage for that app is wiped from your device, so the new installation will cause PushService.subscribe
to re-register to Google Cloud Messaging. If the new registration returns a new registration ID, Parse would have two registration IDs that can be used to send push notifications to your app, and both of them would be linked to the same userName you supplied to subscribe
. Therefore sending a notification to that userName will send it to both registration IDs, causing it to arrive twice.
When Parse send the notifications for you, they should get from Google a response with canonical_registration_id
, which will let them know one of the registration IDs associated with your app on your device is old, and should not be used anymore. Therefore (assuming Parse have a decent implementation of GCM) the next time you send a notification to your device, you should receive it only once.
I found work around which will re-register the deleted installations in Parse again.
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();
}
And when initializing Parse just
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();
}
}
});