iOS 8: App processing silent notification upon launch - ios

I've read about the woes of silent push notifications in iOS, so this will likely be fruitless ... but I figure I'll ask if only to just maybe have an Apple rep see it.
I'm sending a simple APNS message to an iOS 8.1 app:
aps: {
content-available: 1,
priority: 5
}
The app is in the background (read: I have not force-closed it).
Sometimes my app delegate's didReceiveRemoteNotification with fetchCompletionHandler fires, sometimes it doesn't. I can tell because I am monitoring my server and can see if/when a GET request arrives.
I'm currently exploring if there's a correlation with passing weather systems and/or bird migration patterns.
But what's really frustrating is that when I build/launch my app via xCode, here's the console output:
- appDelegate: received notification
- appDelegate: fetching stories
- appDelegate: application launched
- appDelegate: remote notifications approved
**** modal view controller: viewDidLoad
Notice the notification is "received" just as xCode is firing up the app, but before the application launches (?).
It's as though the device is receiving silent notifications but is sitting on them until xCode starts the app. Additionally, I've noticed that when the device is running the app & connected to xCode, silent push notifications arrive very reliably ... which would also suggest some sort of irregularity involving xCode.
Am I doing something wrong? Is anyone else seeing this phenomenon? Or is this just a bug with iOS / APNS?
I'm happy to try out any code snippets or suggestions anyone has! Thank you.

Related

iOS Push Notification Programming - Is there a way to disable push notification on iPhone by programming

A few days ago, I got the issue below.
I've been getting notifications for the past several days. The notification had been enabled, and the app was working (I received push notification) until it suddenly didn't yesterday. And when I open the app, it suddenly asks me to enable notification (please see the picture "iOS Push Notification Permission Alert.png")
iOS Push Notification Permission Alert.png
I guessed that my client removed the app and re-installed, so all permissions were lost. But they said that they didn't.
So, How can it happen?
My concerns are:
If notification was enabled, how can it show the alert above again? Is there a possible way?
If notification was not enabled, why have I received the notification for the past several days?
Under which scenario(s), would an app suddenly disable already enabled notification? Is there a way to disable notification permission for the app on the iOS phone by programming?
I will appreciate your answers so much!
P/s: I read the post disable push notification in app, but it doesn't help for my questions.
This is only possible if the app is reinstalled or the notifications are turned off from the settings.
There is one setting in iPhone which will allow iPhone to offload the unused apps and if this happened, there will be a cloud icon on the left of app title.The app will be reinstalled when you click it again.
And it seems that it will also happened when the storage of iPhone is not enough.
So I guess this happened in your client's phone.
This setting is in both Setting-iTunes & App Stores-Offload Unused Apps and Setting-General-iPhone Storage-Offload Unused Apps.

App not receiving VOIP after x time on iOS 10.3 >

I've got a project I'm working on which uses VOIP notifications to "wake up" apps that have been put into background/terminated. These notifications are sent periodically, and everything seemed to work fine until recent iOS updates.
It appears that after a certain amount of time in which the app has been in the background (happens to some users after 5 minutes, others after 20), the devices no longer process any code after receiving the voip notifications.
I've read a bit in apple dev forums and some other devs are running into similar issues, all speculate if its related to the new iOS updates (10.3.1/10.3.2).
Has anyone else run into this issue, and if so, have you been able to solve it?
Thanks!
This is the new normal on latest iOS versions.
You should begin to use push notifications.
Here is a guide from Apple.
What ended up seeming to be the fix was updating to a newer version of socketio library.
You can try debugging your app in background or in kill state ( terminated state ), so you can get to know that payload from pushkit comes or not or possibly some crash at iOS code end.
Debug pushkit notification in terminated state
Put debug pointer on delegate methods
Go to edit scheme
Select run option then Launch -> Wait for executable to be launched
Send push kit payload from back end
Once you get payload on device
it will automatically invoke and debug pointer will invoke at delegate methods.
Refer

Even with "voip" present in "UIBackgroundModes" in "plist", iOS App does not auto start after device reboot in iOS10

I need my VoIP App to auto start after rebooting the device.
Apple docs clearly mention that :-
(=========EDIT: This is from official Apple docs please have a look at this before commenting or answering that the App cannot be launched without user interaction or silent push notification. Also have a look at Github project below, people have verified this behaviour)
Values for the UIBackgroundModes array
Value : voip Description : The app provides Voice-over-IP services.
Apps with this key are automatically launched after system boot so
that the app can reestablish VoIP services. Apps with this key are
also allowed to play background audio.
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW1
I have ensured that :-
The App was running when the device was powered off.
VoIP is present in the plist and Capabilities section.
Ensured that app the certainly not launched after device reboot by adding logs to a file in the main method and the application:didFinishLaunchingWithOptions: method.
Screen of the device is unlocked at least once, after the device has been rebooted.
I even tried executing this GitHub example App with 36 stars to test
Boot Launch. https://github.com/lithium3141/BootLaunch
But even this App does not restart on reboot when I tried on device.
Hence, this leads me to think if something has been changed recently in iOS10 or am I still missing something here?
Okay, I investigated this a bit further, but first I should point out I did not verify this by actually trying to build a project for this as it would be too time consuming for me now.
I found this (already mentioned in the comments), this, and most importantly this tech Q&A.
What I gathered from especially the various comments of the Apple technicians in those threads, it appears that the behavior of iOS 10 has indeed changed. That means that the same code that connected to VoiP servers in past versions of iOS will no longer do that if you link your build against the latest SDK, i.e. iOS 10 libraries.
Now, in your case, you don't actually need a real VoiP connection, right? You are just interested in the "start after reboot" functionality, correct? At least the demo project you linked doesn't actually do any VoiP connection, the setKeepAliveTimeout:handler: method, for example, isn't even implemented.
I am aware this specific issue is not discussed in the linked threads or addressed in the Q&A, BUT:
It makes sense that, together with the entire legacy VoiP behavior, the reboot feature vanishes as well. Were you to switch to Push-Kit VoiP, your app wouldn't need to be started after a relaunch, it would restart once the next remote notification arrives (and VoiP notifications have high priority so there's supposedly no delay).
Obviously I am deducting the rationale behind this entire thing here and can't guarantee Apple really thought along those lines, but it makes sense: The entire reason for a (legacy) VoiP app to be (re-)launched after a reboot was that it needed to make a connection, i.e. it needed to run some code. With push notifications that is no longer necessary (the OS basically does that for you behind the scenes to get those notifications), so it makes sense that they removed this functionality along with the entire legacy VoiP approach altogether.
You could test this by compiling against the older SDK (i.e. use Xcode 7 as the Q&A suggests) and see whether it relaunches then. That one apple staffer actually explained that the OS does indeed distinguish on the build SDKs for apps, which is totally counter-intuitive to me. Apparently in this case it would decide "hey, this is an older app, so it expects to be relaunched cause its SDK was documented that way" for apps build on Xcode 7 and "Oh, this app is a new one, so I don't need to stick to the old ways" otherwise. Wowsies.
TL;DR: To me it strongly looks like yes, the iOS SDK changed this behavior along with abandoning the entire old, notification-less VoiP approach. Compiling against the new SDKs will result in apps not being relaunched after reboot.
For the record: I can understand the angry people in those threads. While there might be technical reasons for the change, this specific consequence was far from obvious. If a method is deprecated, but the project still compiles and runs I wouldn't expect such a process to fail in that manner. Those apps don't crash, they're just "treated differently by the OS", which not quite the same. At least I would have expected the documentation to be clearer about this in the new SDK.
App will invoke in background when it is in terminated mode only with push kit silent notification and certificates has to be generate for push kit, not with normal APNS notification and normal push notification certificates.
From back end, your payload has to be like this.
$body['aps'] = array(
'content-available'=> 1,
'alert' => $message,
'sound' => 'default',
'badge' => 0,
);
Once you get pushkit payload, then schedule local notification with sound file, your app will be invoked in background upto your sound file plays.( Max 30 seconds ) Til then you have to complete your background task.
Kindly do refer some important details about push kit integration step by step process
https://github.com/hasyapanchasara/PushKit_SilentPushNotification
Life cycle of app - when app is in terminated and push kit payload comes
First of all
didFinishLaunchingWithOptions // will invoke
Then
didReceiveIncomingPushWithPayload // payload method gets invoke
Then if you have local notification
didReceiveLocalNotification // receive local notification
Then
handleActionWithIdentifier // handler method if you have action buttons ( local )
Then if you have remote notification
didReceiveRemoteNotification // receive remote notification
Then
handleActionWithIdentifier // handler method if you have action buttons ( remote )
Note - Without tapping on app icon or receiving push kit payload, your app will never gets revoke/open/repoen automatically. If you want your app is VOIP based and app gets revoke after device reboot. Not possible.

Silent push is not working when app is killed from app switcher in ios7

I am trying to send my updated location to a server whenever I receive a push notification.
I am able to receive a push notification when I killed my app from switcher but my
application:didReceiveRemoteNotification:fetchCompletionHandler
delegate method is not called.
I have spent five days resolving this issue without success. Please, if anybody has any idea about this that would be appreciated.
Thanks in advance!!
Since ios7 apps which are killed from switcher do not receive push notifications.
Documentation isn't clear about this, but apple support confirmed this.
You can see the answer from apple support here:
https://devforums.apple.com/message/925788#925788
another reference:
https://devforums.apple.com/message/873265#873265

Will iOS launch my app into the background if it was force-quit by the user?

I am triggering a background fetch by using the content-available flag on a push notification. I have the fetch and remote-notification UIBackgroundModes enabled.
Here is the implementation I am using in my AppDelegate.m:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Remote Notification Recieved");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Looks like i got a notification - fetch thingy";
[application presentLocalNotificationNow:notification];
completionHandler(UIBackgroundFetchResultNewData);
}
When the app is running in the background, it works fine. (The notification is received and the app triggered the "looks like i got a notification" local notification, as the code above should do).
However, when the app is not running and a push notification is received with the content-available flag, the app is not launched and the didRecieveRemoteNotification delegate method is never called.
The WWDC Video Whats New With Multitasking (#204 from WWDC 2013) shows this:
It says that the application is "launched into background" when a push notification is received with the content-available flag.
Why is my app not launching into the background?
So the real question is:
Will iOS perform background tasks after the user has force-quit the app?
UPDATE2:
You can achieve this using the new PushKit framework, introduced in iOS 8. Though PushKit is used for VoIP. So your usage should be for VoIP related otherwise there is risk of app rejection. (See this answer).
UDPDATE1:
The documentation has been clarified for iOS8. The documentation can be read here. Here is a relevant excerpt:
Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. In addition, if you enabled the remote notifications
background mode, the system launches your app (or wakes it from the
suspended state) and puts it in the background state when a push
notification arrives. However, the system does not automatically
launch your app if the user has force-quit it. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.
Although this was not made clear by the WWDC video, a quick search on the developer forums turned this up:
https://devforums.apple.com/message/873265#873265 (login required)
Also keep in mind that if you kill your app from the app switcher
(i.e. swiping up to kill the app) then the OS will never relaunch the
app regardless of push notification or background fetch. In this case
the user has to manually relaunch the app once and then from that
point forward the background activities will be invoked. -pmarcos
That post was by an Apple employee so I think i can trust that this information is correct.
So it looks like when the app is killed from the app switcher (by swiping up), the app will never be launched, even for scheduled background fetches.
You can change your target's launch settings in "Manage Scheme" to Wait for <app>.app to be launched manually, which allows you debug by setting a breakpoint in application: didReceiveRemoteNotification: fetchCompletionHandler: and sending the push notification to trigger the background launch.
I'm not sure it'll solve the issue, but it may assist you with debugging for now.
The answer is YES, but shouldn't use 'Background Fetch' or 'Remote notification'. PushKit is the answer you desire.
In summary, PushKit, the new framework in ios 8, is the new push notification mechanism which can silently launch your app into the background with no visual alert prompt even your app was killed by swiping out from app switcher, amazingly you even cannot see it from app switcher.
PushKit reference from Apple:
The PushKit framework provides the classes for your iOS apps to
receive pushes from remote servers. Pushes can be of one of two types:
standard and VoIP. Standard pushes can deliver notifications just as
in previous versions of iOS. VoIP pushes provide additional
functionality on top of the standard push that is needed to VoIP apps
to perform on-demand processing of the push before displaying a
notification to the user.
To deploy this new feature, please refer to this tutorial: https://zeropush.com/guide/guide-to-pushkit-and-voip - I've tested it on my device and it works as expected.
Actually if you need to test background fetch you need to enable one option in scheme:
Another way how you can test it:
Here is full information about this new feature:
http://www.objc.io/issue-5/multitasking.html
I've been trying different variants of this for days, and I thought for a day I had it re-launching the app in the background, even when the user swiped to kill, but no I can't replicate that behavior.
It's unfortunate that the behavior is quite different than before. On iOS 6, if you killed the app from the jiggling icons, it would still get re-awoken on SLC triggers. Now, if you kill by swiping, that doesn't happen.
It's a different behavior, and the user, who would continue to get useful information from our app if they had killed it on iOS 6, now will not.
We need to nudge our users to re-open the app now if they have swiped to kill it and are still expecting some of the notification behavior that we used to give them. I'm worried this won't be obvious to users when they swipe an app away. They may, after all, be basically cleaning up or wanting to rearrange the apps that are shown minimized.
This might help you
In most cases, the system does not relaunch apps after they are force
quit by the user. One exception is location apps, which in iOS 8 and
later are relaunched after being force quit by the user. In other
cases, though, the user must launch the app explicitly or reboot the
device before the app can be launched automatically into the
background by the system. When password protection is enabled on the
device, the system does not launch an app in the background before the
user first unlocks the device.
Source:
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
For iOS13
For background pushes in iOS13, you must set below parameters:
apns-priority = 5
apns-push-type = background
//Required for WatchOS
//Highly recommended for Other platforms
The video link: https://developer.apple.com/videos/play/wwdc2019/707/

Resources