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.
Related
I have a weird feature I'm looking to add to iOS and am not sure if it's possible.
I want to send push notifications to all users through a third party and have the client decide whether or not to show it depending on some feature. I was reading that I can modify the notification before it reaches the app and was hoping that I could receive the notification, do some logic and, if the criteria is satisfied, modify the notification to be silent. But I'm not sure if this is possible.
Has anybody been able to do this?
As far as I know, it's not possible to hide a notification after it was sent.
You can modify the payload through a service extension but I'm pretty sure you cannot hide it from the user.
To decide on the client if a notification is visible or not - you'll have to send your notification as silent to begin with and then trigger a local notification.
The problems with that are:
Silent notifications are disabled if the user disabled Background Activity.
Silent notifications have a lower priority and might be throttled after a while.
The purpose of silent pushes is to inform the app of new content to perform a background fetch.
My recommendation is to put whatever logic you want on the server side before sending the notification.
For Android you can decide whether to show the notification or not.
For iOS, you can only modify the way the notification is presented but you cannot stop it from being shown. In order to modify the way the notification is presented to the user on iOS you need to add a Notification Extension Service.
When we receive a push notification while the application is in the background mode, it automatically uses aps and other parameters like Sound, Badge, Alert to generate a notification that appears on top.
What I want is to prevent that auto generated notification let it call didReceiveRemoteNotification and generate my custom local notification and display it on top and notification centre.
The reason behind this is that the message is customised according to situation which is managed locally after some data received in push notification.
I haven't tried anything, because I couldn't find any solution for this kind of scenario. I don't even know whether it is even possible or not.
Any help or other suggestion that could to solve this other way is highly appreciated.
You can have a look to iOS Silent Notifications here
But you have 2 differences cases :
1 your app is in front, the did receive remote is called, and the notification is not displayed.
2 your app is in background, and I don't think the didreceiveRemove is called.
So in the 2 situations, you will not achieve your goal. You shroud maybe try to customize the notification before sending it
Even when your App is in the background, suspended, inactive or terminated it is woken by a Silent push notification.
(Note: the only scenario when the app is not woken by silent push is when it had been killed off by the user from the control centre)
So you can send a silent push, perform whatever operations you need to perform on the data and then generate a local notification which would go in the tray.
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.
I have a VoIP app, where the incoming call notification is very important.
The problem is, sometimes I don't get the push notification (even Apple said it's not guaranteed). But, I do have a mechanism to notice that an call is coming while the app is in the background.
So, what I want to do is.. still use Push Notification as the main handler for incoming call (because it handles the situation when app is closed). However, if the push notification failed to deliver and my app gets the call invite, I will raise a local notification, telling user that you have an incoming call.
My question is... how can I check if a notification is showing before I decide whether to fire a local notification?
AFAIK you can only detect the notification when the user taps on the banner OR if the app is open when the notification comes. So I can't see a way to detect if the notification has come yet or not. Just adding to the pain, push notification is famous for its unreliability.
There’s no API to get any information about the state of your notifications. Since you’re making a VoIP app, you have the option to have it get woken up for incoming data, which would let you post your “incoming call” notification whenever you need it—see the “Configuring Sockets for VoIP Usage” section here.
The previous posting on here regarding deleting notifications from the notification center claim its not possible to delete individual notifications, only all of them.
However individual notifications do get deleted for the reminder app - set 3 reminders to fire in a couple of minutes, when they fire go to the notification center, now select one, after the reminder app launches go back to the notification center and that specific notification has been deleted but others remain. So how is this achieved?
The Reminders app probably fires local notifications. Local notifications can be withdrawn, using cancelLocalNotification: on UIApplication.
(Additionally, push notifications when sent using the enhanced call (first byte is 1) supports an expiry parameter (when sending, not inside the JSON payload) that is supposed to mean that this notification, if not delivered by a certain date, should not be delivered. It is possible that this parameter is also used in a similar way to hide received notifications.
It is also highly possible that Apple's own apps do whatever the hell they want.)
When the user taps on the notification:
If the app was running in the background, you retrieve it using AppDelegate's method didReceiveLocalNotification.
If it wasn't running, then the notification can be obtained with the didFinishLaunchingWithOptions method. You just need to search the launchOptions dictionary for the UIApplicationLaunchOptionsLocalNotificationKey.
If you want to delete specific notifications which have already fired, specially when the user doesn't enter the app by tapping on the notification, then it's probably better to store them in NSUserDefaults so that you can still obtain them later. That approach is explained here.