Prevent show push notification using iOS Notification Service Extensions - ios

I know that with the iOS Notification Service Extensions it is possibile pre-process the push notifications.
I'm searching for a procedure that allow me to skip the push notification is a condition is verified.
For example if the push is not updated anymore I want avoid to disturb the user.
Is it possible with Notification Service Extensions, or other? I've already tried setting the body to empty string but the modify is discarded.
Thanks

When you receive a notification via the Notification Service Extension, you must present a notification to the user. You cannot cancel/skip it. The best you can do is setting notificationContent.sound = nil, which will silence the notification, while still presenting the banner.
Another option is to use Silent Push Notifications, so you can pre-process it and decide wether you want to show a notification or not: then use UserNotifications to present a local notification.

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

APNS Modify Notification to be a Silent Notification

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.

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.

IOS Push Notification disable/enable on the basis of key

Just wondering whether there is any way to disable/enable a push notification when it is received.
For e.g :- When i receive a push notification then i first check in my app whether in notification setting i have enabled or disabled the notification.
There can also be multiple notification settings like
To disable a friend request notification
To disable message notification
So while sending a notification is there any way to append notificationType like if its for friend request or messaging.
Then after checking the notification type and its corresponding setting in the app, showing or discarding the notification.
You can't achieve this just in client side itself. Because once notification arrives it is handled by iOS and displayed in notification centre (or any other type as per user setting). App will not get the notification info, unless it is running.
You can have this as settings in Client and sync it with server to have a check there before pushing the notifications.

How don't allow iOS to start application when specific type of push notification arrives?

I want PHP server send to my iOS application two types of push notifications:
New income message. For this push I want iOS start my application if it was suspended, show badge, play sound, etc.
New friend request. I don't want this push to start my application and I only want to handle if the app is in the foreground.
How can I achieve this? How can I handle different push notifications differently?
I'm not sure if it will work, but you should try for your 2nd scenario to send a notification that contains only custom properties. In this case there will be no alert to display, sound to play nor badge to update, so I think this notification will only reach your app if it's already running.
For the 1st scenario, send a notification with pre-defined properties (alert, sound, badge).
Application-side handling for remote notifications should start with the method in the application delegate protocol application:didReceiveRemoteNotification:.
However, in order to avoid the application launching in the first place, you need to make sure the PHP server crafted notification doesn't offer the option to launch the application.
See the documentation on the Apple Push Notification Service here:
http://developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9
In particular, you want to focus on the content of the aps dictionary as documented in The Notification Payload section. The aps dictionary received can badge your application's icon without opening the app at all.

Resources