I am using Parse iOS SDK. I have problem in resetting the badge count. I am using the tutorial(parse) code to send push notifications. I am using increment for badge, but the badge count keeps on incrementing. I am resetting the badge count in applicationDidBecomeActive: method like this,
- (void)applicationDidBecomeActive:(UIApplication *)application {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveEventually];
}
// ...
}
It just resets the badge number locally. But when I send the push notification next time, it just increments the previous count value and displays it. I guess the badge number in Parse server is not getting reset. Also, I tried to use [currentInstallation saveInBackground]; , but it not working too. Help
Try leaving away the if-statement (if you don't mind unnecessary api requests):
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.badge = 0;
[currentInstallation saveEventually];
It seems that sometimes the installation.badge != 0 check fails.
This is how I solved my desynced badges.
Related
I'm trying to register a device in several Parse channels, but does not work and do not know what I'm doing wrong.
In my table Parse does not appear that the device is in these channels
This is executed when you press a button.
[currentInstallation addUniqueObject:#"CocaCola" forKey:#"channels"];
[currentInstallation addUniqueObject:#"Seur" forKey:#"channels"];
[currentInstallation addUniqueObject:#"ADIF" forKey:#"channels"];
[currentInstallation saveInBackground];
Try the following:
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.channels = #[ #"CocaCola", #"Seur", #"ADIF" ];
[currentInstallation saveInBackground];
Alternatively, look into using subscribeToChannelInBackground
subscribeToChannelInBackground:
Asynchronously subscribes the device to a channel of push notifications.
+ (void)subscribeToChannelInBackground:(NSString *)channel
I am encountering a weird behavior of the PFInstallation of my iOS App. On every app launch I am registering the device to receive push notifications and calling the below code on the method didRegisterForRemoteNotificationsWithDeviceToken:
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
NSLog(#"%#", currentInstallation );
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(#"currentInstallation saved" );
}
if (error) {
NSLog(#"currentInstallation NOT saved" );
}
}];
When I install the app on my iphone the installation is saved correctly with its deviceToken. I am absolutely sure that some days ago i was manually deleting the deviceToken through the Parse Dashboard (to try out things) then on app re-launch the device token was saved correctly again. Same thing for some channels I was using. Today the deviceToken is not being saved, nor the channels. The saveInBackgroundWithBlock succeeds but the deviceToken field is empty. The NSLog of the currentInstallation contains the deviceToken, but it's not saved. While trying to understand the reason for this behavior i found out that if I add the line currentInstallation.deviceToken = #""; just after PFInstallation *currentInstallation = [PFInstallation currentInstallation]; then the deviceToken gets saved correctly. Consume Parse guru explain to me why this is happening and why some days ago it wasn't? I recently upgraded the Parse Framework on this app, can that be the reason?
When i am about to send a push notification to my app from parse, it has an option to increment the app badge. If i turn it on and send the notification, the apps badge will keep on climbing higher and higher. How do i get the badge to go back to 0? See Image here.
Note: If the answer is in code, please answer in swift.
Swift
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
Objective-C
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
UPDATE
to let PARSE know that it has to reset the counter do the following (this also resets the local badge count on the device):
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveEventually];
}
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.badge = 0;
[currentInstallation saveEventually];
Try this.
Code ,above mentioned, change the badge count of app but not update it on server. So when app receive new notification, previous badge number is incremented.
Updated answer for Swift 4:
UIApplication.shared.applicationIconBadgeNumber = 0
I am using parse rest api from my php app to push to ios app. I can see in parse it has been pused to parse successfully
But how come parse not pushing these notificatioin to ios app?
You can do this by following the below steps:-
1) Register for remote notification from iOS app AppDelegate.m
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
2) Subscribe the UDID to channels - Group channels as per your requirement
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation addUniqueObject:#"ChannelName" forKey:#"channels"];
[currentInstallation saveInBackground];
3) Send Notification to the particular channel from your php app.
From the push notification guide, Im noticing that parse recommends setting the device token from within the AppDelegate. Im interested in sending push notifications to certain users, and Im wondering if its possible to move the code for registering a device and their deviceToken within the login code which is found outside of the AppDelegate.
I think you should keep the deviceToken association in the delegate, but after the user logs in, grab the current installation and associate it with the user:
PFInstallation *current = [PFInstallation currentInstallation];
[current setObject:[PFUser currentUser] forKey:#"owner"];
[current saveInBackground];
You can run this code after login/ signup in application
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:sharedInstance.DeviceToken];
[currentInstallation setObject:[PFUser currentUser] forKey:#"user"];
currentInstallation.channels = #[ #"channel" ];
[currentInstallation saveInBackground];