How do silent notification behave once phone got Power off & On - ios

I have a application which tries to grab location of user using silent notification at certain situations. I am able to send silent notification to the phones and able to run the background fetch and get the location back to web-service.
Comparing the user payload of silent notification I am doing some operation(Initiating the location manager & grabbing location from delegate method) while app is in background/suspended mode. This works in all other cases expect one case which is iPhone Power off & turn back ON case.
Problem: According to my observations, Once after phone got power off & turned back ON, The silent notification is being received by iPhones(I confirm this because I hear the sound because I gave sound in payload) but the operation is not happening.
Do any one have better understanding how iPhone behaves once after turned-on. will it do the operation after receiving the silent notification ?
Does code run anything after phone receiving the silent notification ?
I also tried to save the value in UserDefaults before phone getting power off and the value becomes nil after we turned on the power.
Your help, greatly appreciated..!

Related

How does WhatsApp bypass APNS restriction to display multiple push messages received while device was offline?

While working with APNS, I was able to have push notifications work flawlessly while device is online.
For any APNS push I send while device is offline, only the last one is received once the device is back online. This seems to be coherent with Apple's Store-and-Forward design.
However - I did notice, that when sending WhatsApp messages to an offline device, once this device goes online it receives all push notifications (one for each message). This is not something based on collapse identifier, but rather independent push notification for each message.
So how did WhatsApp do it?
Tried using Notification Extension and attempt to post multiple local notifications, but this also fails as extensions are not allowed to do that.
Instead of a normal push notification, use a Background Notification, which will not show anything visible, but wake up your app in background. Use this event, to make api call, get relevant data and generate multiple local notifications.
Note the following from the documentation when you implement application(_:didReceiveRemoteNotification:fetchCompletionHandler:) :
system calls this method when your app is running in the foreground or background
system does not automatically launch your app if the user has force-quit it
you must call the block in the handler parameter (fetchCompletionHandler) or your app will be terminated. Your app
has up to 30 seconds of wall-clock time to process the notification
and call the specified completion handler block
Apps that use significant amounts of power when processing remote notifications may not always be woken up early to process future
notifications
Please read relevant documentation completely before making ANY assumptions about how you think this should work.

Can an app know when user is driving from iOS 11 Do Not Disturb While Driving feature?

In iOS 11 Apple blocks push notifications by giving a notification that you are driving and notifications are blocked.I am curious can we extract that information from iOS and use it in my app?
EDIT: I have already a mechanism with me to detect driving mode but I want to know whether we can access that data or not and want to know what else is there in that data(if that is available).So please dont post links leading to Driving mode detection algos.
Most of the Apps Performing such operations rely on data from Accelerometer. All smartphones now a days, including iOS has builtin Accelerometer sensor in them.
The idea to predict whether a person is driving or not is to infer speed of the user movement from the data provided by this sensor and perform operation based on that.
All SDKs provide functions that can use this data. Hence, it will be available for you also.
There is no public API that manage Do Not Disturb Driving, if you want to know user activity you can use CMMotionActivity:
var automotive: Bool
A Boolean indicating whether the device is in an automobile.
On devices that support motion, you can use a CMMotionActivityManager
object to request updates when the current type of motion changes.
When a change occurs, the update information is packaged into a
CMMotionActivity object and sent to your app.
All push notification are managed by the system, if they are blocked you can not access to their data. Maybe if you need some notification to do something in your app, you can use Silent Push Notification:
Configuring a Silent Notification
The aps dictionary can also contain the content-available property.
The content- available property with a value of 1 lets the remote
notification act as a silent notification. When a silent notification
arrives, iOS wakes up your app in the background so that you can get
new data from your server or do background information processing.
Users aren’t told about the new or changed information that results
from a silent notification, but they can find out about it the next
time they open your app.
For a silent notification, take care to ensure there is no alert,
sound, or badge payload in the aps dictionary. If you don’t follow
this guidance, the incorrectly-configured notification might be
throttled and not delivered to the app in the background, and instead
of being silent is displayed to the user
But maybe this kind of notifications are blocked too:
Do Not Disturb While Driving feature and it will basically turn off
your phone without actually turning it off, so no notifications of any
kind will be able to get through.

iOS Firebase/APNS push notification sound according to local time or timezone

I am using FCM to send iOS push notifications for an app that is used worldwide. Different users are in different time zones.
Is there a way to specify ,that when delivered at worktime local time Mon-Fri (8:00 - 17:00) it will have sound, but any other it will be without sound. I do not want to disturb the user at night.
This question is mostly about the worst situation when my app isn't running/backgrounded/suspended (for example after reboot) and/or offline
.My app uses background fetch, but that doesn't guarantee that it will be running at the delivery time of the push notification.
The problem is i do not know in advance when the push notification will be delivered. If i knew i would specify the sound key in the JSON.
The user could be offline when the push notification is sent.
For example, i send the push notification at night without sound, but the user enables internet connection at day and receives the notification without sound (because it was sent in the night)
Scheduling the sending time at the server won't solve this, because we cannot control the delivery time.
If it is possible using directly APNS without Firebase then that would also solve my problem.
Thank you very much.
Clarification:
When my app is running i have enough control to decide according to local time if the sound should be played. The question is aimed at the other case when my app isn't running/backgrounded/suspended.
I am using silent data notifications (JSON key data with content-available=1) to deliver all neccessary info, but when my app is not running there is only one way to send a notification that will be shown - that is a Display notification (JSON key notification)

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 GCM push notifications does not always received in background

I'm working on getting background notifications to work on IOS with GCM - non-background notifications are already working.
In background notifications sometimes come and sometimes not.
I used conten_available = 1;
I successfully obtained a Registration Token and subscribe to topic:
Registration Token:
nU8ef5ZzonI:APA91bFaazXpqgI2wKTCujMaLLIZaKOmdpPAz2_WRc3V54d4eEI8p8VeAUZLwMAQ_8iaDDQ4XJAS44dFyIQkXcZ8cJjVdEGUEgnNOtrqxBKFHDTtPOUf2xT28vRprdStdVNzvrBFCQ3
Connected to GCM Sep 2 17:37:24 iPhones-iPad News[4201] : Already subscribed to /topics/news
But often in the backgrounds the application does not receive notification.
Or, the notification may come all at once old and new.
Why notification work so unstable?
You said "But often in the backgrounds the application does not receive notification. Or, the notification may come all at once old and new.".
Meaning not that they don't come, but they are delayed. Background pushes often are not immediate delivery and may take several hours.
If the user force quits the app they will not be delivered at all.
If you plug your iPhone into a power charger (or connect it to a computer via a charging usb cable) and try again you will most likely find they are delivered immediately. This is because starting with iOS8 background notification delivery is tied to power saving and hence why you will see them delayed and bunched together when they are delivered.

Resources