Deleting notifications from the notification center revisited - ios

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.

Related

Who handles the notification in iOS? - really React Native

I have a doubt, I've searched and didn't find the answer, I have implemented notification in my app, is it possible to have a cancel notification in the app or it is only a system option?
When the user opens my app on the third screen it asks about sending notifications, but what if the user doesn't want to receive those notifications anymore, does he need to go to iOS settings or can I do this from the app?
Thanks for your time
If you've used react-native-push-notification or #react-native-community/push-notification-ios then there has a way to remove notification from notification center.
you can use this code to remove all natification
PushNotification.removeAllDeliveredNotifications();
Removes the specified notifications from Notification Center
PushNotification.removeDeliveredNotifications(identifiers);
check out official doc for more detail
packages:
https://www.npmjs.com/package/react-native-push-notification
https://www.npmjs.com/package/#react-native-community/push-notification-ios
If you no longer needed to receive notifications in your app, it's better to handle the logic in your backend. When the user denies permission, remove the push token from the server. If you only want to remove certain types of notifications, instead of removing tokens, you can set a flag on backend logic to enable/disable certain notifications.
If you really want to do this on the client-side, you can use data-only notifications of FCM or a similar kind, where you have the control to present notifications as local notifications. Basically, a background listener will be triggered when you receive a data-only notification and OS won't present notification. You need to create a local notification and present it according to the data you receive. Here you can add your logic to filter notifications based on User's choice

How can I cancel or stop push notification from a client?

I have a webserver that sent a client a push notification. The client can intercept the push notification with UNNotificationServiceExtension on iOS 10 and change the content. Now I want to cancel a push notification from showing on client side on certain notifications. How do I do that? I tried to do
self.contentHandler(nil);
but it didn't work. How to do it?
It look like it might not be possible, but not sure. From the docs:
You can modify any of the content from the original request. You might
customize the content for the current user or replace it altogether.
You can use this method to download images or movies and add them as
attachments to the content. You may also modify the alert text as long
as you do not remove it. If the content object does not contain any
alert text, the system ignores your modifications and delivers the
original notification content.
https://developer.apple.com/reference/usernotifications/unnotificationserviceextension/1648229-didreceivenotificationrequest?language=objc
Calling the block with nil just displays the original notification?
It's now possible since iOS 13.3. You need to pass an empty UNNotificationContent to contentHandler (not nil) and add the new filtering entitlement.
// Determine whether you should suppress the notification.
let suppress = myShouldSuppressNotification(request: request)
if suppress {
// Don't deliver the notification to the user.
contentHandler(UNNotificationContent())
} else { ///...
Reference : https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_usernotifications_filtering
I think the easiest way would be to send every push notification as a silent notification by default and only show the ones that you want to show. When you get a silent notification in
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
You check your notification and determine if it should be displayed to the user. If not, you do nothing with it. If it needs to be shown then create a local notification and display it UILocalNotification (iOS 9 and earlier) or UNNotification (iOS 10).
I should add this has the disadvantage that no notifications will show if the app is not resident in memory (because the app needs to make the silent notification into a local notification), but if you were to take the opposite tact then you have the problem that something might be shown that shouldn't be show.
On iOS 10 and above you can send you a "silent" notification:
Apple doc: configure silent notification
Then you can decide if the notification should be shown to the user, if so, just create a user notification and show it:
Apple doc: local and push notifications
#Jorge Arimany's answer is to send the silent notification to wake up devices to do something, I think it's not the solution this user wants. And I think we still will get the general notification if we do nothing in our UNNotificationServiceExtension class
Pre iOS 10: I dont think it is possible. Here are the reasons. Once when the server sends a push notification to the client to Apple APNS server to send it to the clients, you lose the control of the push notifications. This is one of the reasons apple doesn't always promise 100 percent delivery of the notifications. This implicitly tells you that the Push notifications on the iPhone is handled by the iPhone operating system rather than your App.
Even the push notifications displayed in the Apple iPhone Springboard/dashboard is not in your control.
Users can either control whether he wants to receive the notifications or not.
So i don't think you can control the notifications partly after it is sent from your server.

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.

Cancel Local Notifications after app is unused for a certain period of time

I am wondering if it's possible to do what the titles says. I have an application that has a refill reminder to refill your prescription drug via local notifications. I have seen that some apps (pill reminder apps mostly) push a notification if you have not taken your pill, or have not answered back to that notification, and was wondering if I can do the same if a user doesn't open/interact with the app after a certain period of time.
I have not began implementation but have thought about this thoroughly. What I am thinking of doing is having some sort of flag when the app is opened that removes that local notification and sets a new one once the app has gone in the background/inactive. The local notification would be set to three months from when the app has gone in the background/inactive. The question then becomes, how do I handle canceling all notifications after this notification has been received, regardless of whether the user opens the app at that notification or not?
If the user opens the app on that notification, I can have a check the method application:didReceiveLocalNotification and then handle the case where that local notification has been set and then use [[UIApplication sharedApplication] cancelAllLocalNotifications]
But if the user does not tap or open the app, how can I check and cancel all local notifications?
Sorry if this is a bit long or worded weirdly (sorry not good with words and explaining things). Let me know if you need more info or better explanation. Thanks in advance!
If I understand your question correctly, you want to know how to keep notifications from repeating when the user does not respond to a notification by opening your app.
You might consider configuring your local notification not to repeat. Instead, you might reschedule notification batches each time the application is launched.
Alternatively, if your application has a server-side component, you can use push notifications on iOS 7+ to wake your app, briefly. There is no equivalent to this behavior using UILocalNotification.

How can I handle unread push notifications in iOS?

I have a iOS 5.1 application that registers to the APNS service to receive notifications. The register is successful and I receive the notifications correctly. The problem comes when I try to handle the notifications.
Once the application is running, the method didReceiveRemoteNotification in the AppDelegate is called correctly and so the notification is handled as intended. This, however, only happens when the application is running on the foreground.
However, when the application is running on the background or is simply stopped, that method is not called. I've read that you should add some lines to the method didFinishLaunchingWithOptions method to obtain the notification from the userInfo dictionary, and handle it. This works just fine, but ONLY when the application is opened by clicking on the notification at the Notification Center. This means that if you open the application by clicking on its badge, or simply by changing context if you were running it on the background, the app never realises that a notification came in. Additionally, if more than one notification was received, we can only handle one of them at once by clicking on the Notification Center, which is a pain :-)
Is there any way to read the pending notifications in the Notification Center? I know there is a way to flush them using the method cancelAllLocalNotifications but I haven't found a way to just read them. And I really need to handle all of them. I thought of implementing a communication protocol with the third-party notification server to retrieve the information again when the application comes to the foreground, but since the information is already in the operating system I would find it strange if it's impossible to access it somehow.
So, does anybody know a way to do it? Thanks in advance.
When a push notification arrives and the user clicks 'cancel', your app has no way to read that push notification again. You have to implement a separate functionality (most probably on server-side) to fetch a list of notifications sent to this device.
For example, if a chat functionality is provided in your app and you send chat messages via push notifications then you should also keep chat messages on the server. If a user clicks 'Cancel' on any push notification then that chat message will not be displayed on the iOS device. In that case when a app comes in foreground later, you make a call to the server and fetch all the past chat messages (sent via push notification).
Ok, So a possible solution would be to have another database table with the messages in with a 'read' flag and a messageID field? Which by default the read flag is NO, then when the app successfully reads this and displays, it updates the flag to YES?
And with only 256 bytes to play with, what sort of ID field length would be necessary?
Edit,
Executed this plan and its working successfully.

Resources