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
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.
In my aplication I have 2 types of notifications and the user has the option to disable or enable each one inside the app. For example if the user wants to receive only the type1 notification, he will go to the settings and he will disable there, but he will receive notifications of type2. I saw in Spotify and the user is able to disable notifications by type.
My problem is that, from what I read here and here I'm only able to do it server side. I can't do it on client side is that correct?
The unregisterPushNotifications disables everything. So in my server I would have a "flag" to send it or not.
If I use FCM (Firebase) will I be able to disable notifications by type or will I have to make the same thing as if I user APNs?
Take a look at the service you are using to send push notifications, many will include the concept of "channels" or "groups" that your users can subscribe to. But, yes, this is managed on the server side, not through the push notification registration.
For those who wonder how I managed to do it.
The alternative I found was using APNs to send silent notifications and when I receive a notification, I create a Local Notification.
I receive the silent notification, it triggers the method in AppDelegate and I read the data of the notification. If it is the type I want it to show up, I create a local notification and make it show up in the system. It was the best solution I found.
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.
Is there any way to know if a local notification was dismiss from the Notification Center (for example by pressing the x button)?
Is it possible to get a list of all the notifications currently visible in the Notification Center?
No it's not possible to get list of notifications from any of APIs from Apple.
And no there is no feedback from ignored / closed notifications.
If you want to implement logic like that, I would propose to build this type of logic based on your own servers.
When you send a notification you keep information about it on server side.
When you open the app from notification (that it's doable) you send information to a server side.
When you open the app not from notification you can have a logic on a server side checking if there were any notification that were not confirmed from the last time you opened the application.
Keep in mind that notifications are not always delivered - it's not guarantee that user will get them and if use has notifications turned of for your application - all notifications will be treated as ignored.
To improve that logic you may want to send information about user permissions for notifications to your servers - which still is not guarantee that he didn't disable them in settings :(
Long story short - never ever build any of your application core functionality based on notifications.
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.