didreceiveremotenotification Not Called In Foreground When Notifications Disabled - ios

If user does not give permission for notifications, didreceiveremotenotification is not called when a remote notification is received while the app is in the foreground. I do not want to use a silent notification because when the app is in the background and user allows notifications, I want the alert notification to appear. But when in the foreground, I want to handle the notification data myself, even if the user has opted out of notifications.
How can this be achieved?
EDIT: I see that device registration token is not created when user disables notifications. But how would silent notifications work then?

I have found the following SO answer: What is difference between remote notification and silent notification in iOS?
In short, to achieve what I wanted, I needed to enable Remote Notifications in the Bacground Modes of plist. Then I was able to register a token even if the user opted out of receiving notifications.

Related

Can i receive messages with FCM without notification permission?

I want to know if I can receive payload from FCM in my iOS app, even if the user doesn't allow notifications for my app. I don't want to show them as notifications, i just want to handle their payload.
Does background messaging work without permission?
Does foreground messaging work without permission?
The Firebase Cloud Messaging APNs interface uses the Apple Push Notification service (APNs) to send messages up to 4KB in size to your iOS app, including when it is in the background.
Notification permission is required for both background and foreground work (even if your push notification is a silent one i.e no notification needs to be displayed)
You still need the user to allow the notifications, later on, you can implement the handling as silent or not, up to you.
In order to receive the payload you need, it must come from a push notification.

APNS: Receive non silent notifications if app is not running?

As I can see in the APNS documentation, silent notifications are handled in didreceiveremotenotification if the app is not running, but they have an low priority. So sometimes my iOS application doesn't receive silent notifications.
Does iOS show non silent notifications, if the app is not running (not in foreground, not in background)? And will a non silent notification trigger the didreceiveremotenotification?
For non silent notifications,
didreceiveremotenotification will be triggered if the app is in active or inactive state. Not when terminated or suspended state.
In case of terminated or suspended state when user taps on notification app will be launched by calling didFinishLaunchingWithOptions and launchingOptions will have the payload as Dictionary.
In case you provide UNNotificationServiceExtension then iOS will call didReceive(_:withContentHandler:) on receiving the notification and you can use it to customize the content of a remote notification before it is delivered to the user.
read:https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension
In case you provide UNNotificationContentExtension then iOS will call the didReceive on receiving the notification and you can use it custom load notification content.
Read : https://developer.apple.com/documentation/usernotificationsui/unnotificationcontentextension
P.S:
Normal notifications can not be used as an alternative/work around to silent notification just because you cant use silent notification in app terminated state.
Silent notifications are intended for syncing the client app with the updated content available at server. As this can be done without the explicit user interaction silent notification can be used.
Silent notifications must contain content-available key and must not contain alert, sound, or badge keys.
read : https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
Where as in case of normal notification, there is no way to hide notification banner/alert/sound unless the user setting on phone says so.

How to remove new Notifications from banner?

Is there any way to handle remote notification payloads before they’re delivered and display in notification centre?
With push notifications of default type there is no way to filter out notifications if the app is not in foreground.
The possible solution to this is to use VoIP push notifications in conjunction with PushKit.
VoIP pushes are always waking up the application, however they are not presented to the user and don't modify app badge value, so it's up to the developer to show a local notification upon the received VoIP push and to deal with app badge.
There is a technique with additional silent push that for example Facebook is using, to delete notification on iOS device when the message has been read on desktop web site. It's described here: https://www.pushwoosh.com/docs/deletable-ios-push

Any way to know that a local iOS notification was displayed to the user if the user did not interact with it

I am trying to write a notification tracking system for my app. My understanding is that a scheduled local notification is guaranteed to get delivered but that my app won't get a notice that it was delivered unless the user interacts with that notification in some way. Am I missing something? Is there some event or notification sent to the app simply to indicate that the notification appeared?
No, I don't think you have any way of knowing about the "notification not taken."
A Jailbroken device would be a different matter.
There are silent push notifications, and I haven't looked at the new notifications API in iOS 10, so I guess it's possible Apple added a silent local notification for iOS 10...
Actually there are 2 scenarios:
App in background: When a local notification is delivered by the system, app is not notified, only if user interacts with notification view app is awakened.
App in foreground: App is notified when a local notification is delivered by calling didReceiveLocalNotification method.
Local notifications are guaranteed to be delivered only is user allows this (app will ask user to allow to receive local notifications)

Apple push notification-like service, only when running in foreground

I have an iOS app that needs to update its content while running in foreground automatically. My app does NOT need to update if in background.
There is a existing way to do so, which is APNS(Apple Push Notification Service).
Because I don't want users to see notification message while in background, using push notification without alert or message might be a solution.
However, if using APNS, iOS would ask users to confirm if they want to receive notifications by my app. I think that users may be confused when being asked by the OS since my app does not actually push notification to users.
The current method I use is keep pulling my API every 30 seconds to see if new content is available. This method would fail if there are too many users.
Is there any 3rd party push-notification-like service that provides notification while app runs in foreground only? (no need to get notification while in background)
You can use Silent notification for that, in this
In the WWDC 2013's "What's New with Multitasking" presentation, there is a section about Silent Push Notifications. if you send the APS payload with just the content-available set to 1, users will not be notified of the notification.
And the notification arrives in application:didReceiveRemoteNotification:fetchCompletionHandler:
Your payload is like
{
aps: {
content-available: 1,
sound: "default"
}
}
In case of push notification, it is necessary for user to accept push notification on application 1st run. You can set a silent push notification also and for this user will not get any alert of getting a notification during application run loop.
If you want to avoid push notification, then you can only set a NSTimer that you are doing already.
There can be a 3rd case, Application only sync with the server when it comes to foreground. And for this you can refer to my this post.

Resources