iOS 7 - didReceiveRemoteNotification: not triggering on background - ios

I know that this question is tipical but I'm missing something and I don't kwon what is.
I have a simple application, in iOS 7. Using Xcode 6. I'm developing in objective-c.
I want to receive push notifications and update my app icon badge to (1) but I can't trigger the didReceiveRemoteNotification method when the app is in background so I can't set the badge number. This is not my first app with push notifications and badges but I can't do it...
My code:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
NSLog(#"RECEIVING NOTIFICATION");
When i'm on the app the badge is correctly setted and the log is printed, but if I'm in background mode the method is not triggered.
My payload APNS
{"aps":{"alert":"New push","sound":"default"}}
I want to set the badge manualy to 1. I don't need it on de payload.
Any suggestions?
Thanks

The remote notification will set the badge automatically according to the number of "badge" in aps.
For example:
{"aps":{"alert":"New push","sound":"default","badge":1}}

A bit late, but it might help someone else.
You can trigger - application:didReceiveRemoteNotification: when your app is in background by following this two steps:
Set Required background modes to App downloads content in response to push notifications in your Info.plist file
Add "content-available": 1 to your notification payload.
This will call - application:didReceiveRemoteNotification: and allow you to handle notifications in background (i.e, refresh the content of your app, etc...).
Hope this helps!

Related

Update badge icon

i try to update badge icon, every time when when the app (in background)receive a push notification.
i used that in "didReceiveRemoteNotification" and "didFinishLaunchingWithOptions"
[UIApplication sharedApplication].applicationIconBadgeNumber++;
If the app is in foreground and receive few push notifications, when the app come back in backgorund the icon's badge is updated with the number of push notifications received.
But if the app is in background the icon's badge is allways 1 like in payload even the app receive many notifications.
Do you have some ideas? Thanks in advance
The push notifications that are sent to your device should have the badge numeric value which will be et automatically when push is received.
The server should somehow handle the number of your unread notifications and it sends you the correct display number in this field.

App doesn't receive push notifications in background mode

I'm developing an iOS app that receives well the remote notification when it is in the running state, but it doesn't receive any thing and the notification banner is not showing up in the background state. I set notification stale to banner, background fetch and remote notification are checked in the background modes menu, I'm using xcode 6 and didReceiveRemoteNotification to handle the notification.
Any ideas on how to fix this issu please ?
Check if Background App Refresh is enabled for your application.
Then check what callback method you're using, this must be
- (void)application:(UIApplication *)application didReceiveRemoteNotification:fetchCompletionHandler:

How can we remotely dismiss a Push Notification?

My app daily broadcasts a Push Notification (PN) to all users which becomes irrelevant after 4 hours. Is there any way I can remove that notification on every user's notification centre that has not tapped it within those 4 hours?
I used to think this is not possible yet, but became hopeful after seeing the Google's Hangout app behaviour - It sends PN to Mac & iOS... and if I read the message on Mac, it automatically immediately removes it from iOS' Notification Center.
I did extensive research on google, surprisingly found nothing on this - just one question here which has been duly closed!
The trick is to make your app support background fetching and handle the push notification when you app is in the background.
Then in the application:didReceiveRemoteNotification:fetchCompletionHandler: set the application badge to 0 so that all you push notification are removed from the notification center.
Send a special push notification where there is not data displayed to user but does contain a an command to reset the push notification state.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if([[userInfo objectForKey:#"reset"] boolValue]){
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
}
}
It is only possible to dismiss a notification once the user has tapped. You can't dismiss on hide it if the user hasn't tapped it to open.
If you want to dismiss an opened notification, you can try cancelLocalNotification: to dismiss a notification that is presenting an alert at present.
According to apple documentation:
You can cancel a specific scheduled notification by calling cancelLocalNotification: on the application object, and you can cancel all scheduled notifications by calling cancelAllLocalNotifications. Both of these methods also programmatically dismiss a currently displayed notification alert.

how to get all notifications when app is in background?

I can get the notification by calling didReceiveRemoteNotification , but if the App is running in background and there are 5 notifications, how to get all notifications when App become active?
Unfortunately, there is no way to do this. The data from a notification can only be pushed by clicking the individual notification. More info here: iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

ios remote notification not beeing marked as read

Ok, so I've wrote a small app that receives remote notifications that I handle in didReceiveRemoteNotification and I also check the userDict passed to didFinishLaunch.
However, the notifications is sill listed as "unread" in the notification-center on iOS 5 (there is a tiny blue dot to the left of them). How can I make it so that after a user has clicked a notification it either goes away, or it's marked as read?
I don't think there is a way in iOS 5.0 to only remove one entry. Quoting iOS SDK Release Notes for iOS 5.0:
Springboard
Push and local notifications for apps appear in the new Notification Center in iOS 5. Notification Center displays notifications that are considered "unread.” To accommodate push and local notifications that have no unread status, set your application’s badge count to 0 to clear that app’s notifications from Notification Center.

Resources