Counting push notifications on iOS (deal with cancel from notif center) - ios

I'm trying to understand on how to deal with a particular use case with push notifications.
Let's suppose that I have 4 push notifications in the notification center waiting for an interaction, the server keeps track of the badge number. The user picks up one, in the -application:didReceiveRemoteNotification: I decrement the badge and communicate to the server the notification back to make it decrements its "badge" number and mark the notification as read.Everything works fine.
I'm not able to deal with that case. Let's say that the user deletes from the notification center one notification and then opens the app by tapping on another, I will have an incorrect badge number on both server and app, since deleting a notification it doesn't seem to affect the badge number.
Is there a way to understand which remote notification has been removed?
It seem that for a more fine grained control is better to use a mix of silent push and remote notification.

Related

Getting notification badge number based on delivered notification swift

I have an application which receives Push Notification and these notifications are delivered properly to the device. However when these notifications are delivered badge does not count. I have read online that one sends badge number with the payload which I understand but does not work for my case. Notifications are delivered based on different instances and I want to be a able to increase the badge number based on the available notifications the user has not opened his device to view.
With payload, there is no way to know if a user viewed the notification already and zero it. I am trying to avoid setting a badge in the payload to maybe 7 whereas the unopened notification on the device is one.
Since push notification are handled by iOS and not by ios app you can't change the application badge on receiving a push notification.
Though you can send the badge number in the payload of the push notification as you already send, so you have to do the calculation on server side.
Go through Local and Push Notification Programming Guide and especially the The Notification Payload.

Remove but not cancel local notification

cancelAllLocalNotifications() will as the name says, cancel all local notifications.
Consider that the phone has received a couple of notifications while the screen was in locked state. And we are only have interest in notifications received when the user actively are using the phone.
Is it possible to only clear these notifications from the list in the Notification Center, but without cancelling them?
If you can use identifier for description your every notification, than you can only one notification removing, after this process you can setting notification again. I think this kind a trick working for your situation.

Does a App Icon badge from a local notification get overridden from a Push Notification payload badge?

My app will run in the background for ~10 minutes before the system shuts it down. During this time, if the user receives an event, I handle it using Local Notification to inform the user and display a badge icon. Once the App is no longer running in the background a push notification is sent, but when a push is sent, it overrides the current badge count from the local notification since a payload count is attached with the push.
Is there a way to deal with this?
Looks like the OS is responsible appending badge numbers from push payload. It simply places whatever count the payload has associated with it.

How to handle badge count If user delete/clear the notification from the server in ios?

Currently I am implementing an ios app with the server push notification.
I am successfully able to send push notifications with badge.
Badge count will be incremented from the server. If user opens the application from notification, it will reset count on the server.
Now the query is if user is not opening application from notification and deleting/clearing all notifications.
That means there will be no notification so when app will be opened from the background it wont find any notification and so it will not reset the count on server and also not the badge value on app icon.
So how can i resolve this issue?
Use custom notification icon and maintain read and unread counts with in the app,you will not manage badge counts from push notifications when user remove and open notifications in the app.
From what I understand, the only way your app is getting the relevant data is if the user clicks on the notification. I'm assuming you're sending a custom payload of some kind. Though I don't know the nature of your app at all, I would say this is really not the way to go specifically because of the problem you're encountering - you have no way of knowing if/when the user clears notifications in the Notification Center and if they do, that data is forever lost to your app.
I would recommend your app request the new data from your server upon launch - ie/ get anything new since last time the app was opened - this way it's independent of whether the push was received or not and depending on what your server sends back, you can clear the push count (or not).
Whenever application hits appDidBecomeActive delegate, clear the badge count to zero.

Apple push notification-like service, only when running in foreground

I have an iOS app that needs to update its content while running in foreground automatically. My app does NOT need to update if in background.
There is a existing way to do so, which is APNS(Apple Push Notification Service).
Because I don't want users to see notification message while in background, using push notification without alert or message might be a solution.
However, if using APNS, iOS would ask users to confirm if they want to receive notifications by my app. I think that users may be confused when being asked by the OS since my app does not actually push notification to users.
The current method I use is keep pulling my API every 30 seconds to see if new content is available. This method would fail if there are too many users.
Is there any 3rd party push-notification-like service that provides notification while app runs in foreground only? (no need to get notification while in background)
You can use Silent notification for that, in this
In the WWDC 2013's "What's New with Multitasking" presentation, there is a section about Silent Push Notifications. if you send the APS payload with just the content-available set to 1, users will not be notified of the notification.
And the notification arrives in application:didReceiveRemoteNotification:fetchCompletionHandler:
Your payload is like
{
aps: {
content-available: 1,
sound: "default"
}
}
In case of push notification, it is necessary for user to accept push notification on application 1st run. You can set a silent push notification also and for this user will not get any alert of getting a notification during application run loop.
If you want to avoid push notification, then you can only set a NSTimer that you are doing already.
There can be a 3rd case, Application only sync with the server when it comes to foreground. And for this you can refer to my this post.

Resources