How to make a remote notification conditionally displayed? - ios

In our app environment, multiple shifts of employees use the same phone. It is possible for our app to receive notifications intended for a prior user. Our notification extension can determine if the notification is for the current user. We'd like to hide the notification if its not intended for the currently logged in user.
Notification Extensions, (UNNotificationServiceExtension) apparently do not allow the notification to be hidden. It can be modified but will always be presented to the user.
Why?
Is there a way around this?
I've tried sending silent notifications and generating local notifications. It works, but Apple/iOS starts throttling it after a few notifications and it becomes very unreliable.

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.

Do Push Notifications with content-available work for users that have disabled Push Notifications?

I'm looking into reliable ways of updating an iOS app with content of critical importance, when instructed to do so by a server.
This would have to trigger regardless of the state of the app (foreground, background, not running, etc). Most sources seem to indicate that Push Notifications with the content-available: 1parameter are able of doing this.
My question is, what happens if the user either presses Cancel when prompted to allow the app to receive push notifications, or turns them off afterwards ? Is he only not going to receive alerts anymore, or will the push notifications be entirely ignored (including the content update) ? And if so, is there any other iOS feature that would reliably allow me to update the app's content (regardless of app state and without the user being able to disable it) ?
This is about iOS 7 and above.
If user disallows the push notifications , you can not send them the notifications forcefully.
What kind of data you want to update afterwards?
Most of the data can be changed dynamically by web services...

iOS Reachability when app not running

a simple question: is it possible to get a message, notification or similar when the internet connection is available when app is killed or not running?
For my purpose, I need a way to synchronize all my notifications because APNs can send only the last message.
EDIT:
I'm wondering how some apps (e.g. whatsapp) are able to sync their notifications when the internet connection is up. If I kill whatsapp, I can receive multiple notification when internet connection is reachable, but the APNS server provides only last message and, for this case, I'm not able to send silent notification. If I should develop a chat application, what are the best practices to work with Apple notifications?
If you send a push notification with a title, text, sound and/or badge property while the app is suspended (was killed / force closed), the device will still receive it, e.g. will show the text as a notification, play a sound and/or change the badge count.
However, your app won't be launched or woken up in the background in this case, so you have no way to handle the notification before the user taps on it. (See this question:
Will iOS launch my app into the background if it was force-quit by the user?)
So if the app was force closed by the user, your only option is to send a notification to be displayed as it is and if the device is offline, only the last notification will be received and displayed by the device.
For more control, you could use silent push notifications to implement "push-to-sync". In this case, the push notification only signals that there is new data to be fetched. The app (if not force closed) loads the data from the server then and triggers local notifications with the right data in the right order. But this won't work, if the app was force closed.
Apple push notifications have a lot of restrictions, so you won't be able to implement a perfect solution. In my opinion, it's fine if the user gets only the last notification when the device gets online after being offline for a while. At least he is informed that there is a new message and after opening the app, he can see the other new messages too. For the "push-to-sync" scenario, I would say that the user has no right to expect that the app works as desired, if he force-quits it.
Push notifications were never intended to be used in the way they are used by a lot of apps by now. E.g. they shouldn't contain sensitive data such as a chat message. They were intended to inform the user that there is new data for the app, so he can launch it to see the new data. E.g. instead of sending the actual chat message text a push notification should just contain the text "You have a new message". Then, you don't have the problem you described. Of course this is a terrible solution in terms of usability.

iOS execute check before push notification is received

I am writing an application that utilizes Apple's Push Notification Service. Some of the push notifications are based on the users location and should only be delivered if the user is a certain distance from an object. I don't want to continually update the user's position to my server and do the check that way, first, because of security reasons and second, to cut down on the network usage. Is there a way, when the push notification is received by the device, to do a check before the user is notified, and if it doesn't meet the criteria, discard the notification? Thanks for the help!
Nope, sadly you can't execute any code on the client side without the user clicking on the notification when the app is not launched. You'll have to do your check server-side to decide wether or not sending a push.
Push notifications that are received while you app is not running (the most typical case) is outside your control. Once they are sent, they will be received and shown to the user (assuming the user has granted permissions)
You do have control over push notifications if they are received while your app is running.
Maybe you could use local notifications (notifications that are generated and received from user's device) instead. You would have full control over when they are generated.
Bear in mind, background processing in apps is disallowed except for 4 things
Location tracking (You can subscribe to trigger code when user changes geolocation)
Alarms
Playing music
Voice over IP
Anything outside these cannot be executed in the background.
Starting with iOS7, your code can be run, I quote, "roughly the same time that the user sees the notification":
Multitasking Enhancements
Apps that use push notifications to notify the user that new content
is available can fetch the content in the background. To support this
mode, include the UIBackgroundModes key with the remote-notification
value in your app’s Info.plist file. You must also implement the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method in your app delegate.
See more info here:
https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW10
and here:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW57
I haven't tried it yet but I wonder if you send a push notification payload which doesn't have the standard "alert", "badge" values in "apns" but in a custom value, it would still call your code but without displaying the push notification.

Resources