iOS Remote Notification processing - ios

Quick question, I am working on an APNS enabled app and I just want to know if there is a way that when the app is not open (not in background) and the app receives a remote notification;
Can I open the app in the background, unbeknownst to the the user, and do do what needs to be done.
I have looked at the docs but it doesnt explicitly say that this can or cannot be done (from what I read), it says that the user has to open the app manually (either from tapping an alert or the app icon).
I think it is clear what I'm looking to do however i'll put an example aswell.
Example)
The default Mail app alerts the user of new emails with a badge icon, showing the number of new emails, aswell as a sound. The user must then tap the app icon to open the app and download the new emails.
I want to be able to have the badge and sound but open the app in the background (silently) and download the new emails, so that when the user does get around to opening the app the emails will already be there, ready to read.
Thanks for any help.

unfortunately, what you describe is not possible.
the only way you could get something like that is to build a voip app. see the relevant section in this document.
Implementing a VoIP Application
A Voice over Internet Protocol (VoIP) application allows the user to make phone calls using an Internet connection instead of the device’s cellular service. Such an application needs to maintain a persistent network connection to its associated service so that it can receive incoming calls and other relevant data. Rather than keep VoIP applications awake all the time, the system allows them to be suspended and provides facilities for monitoring their sockets for them. When incoming traffic is detected, the system wakes up the VoIP application and returns control of its sockets to it.
anyway, I doubt that your app would get into the App Store, then...

Related

Xamarin iOS App is not able to execute code when it is already minimized

I have a chat application, developed on the Xamarin.Forms platform, in which users can chat with each other. I have managed c# code and UI both in the shared project.
I have been facing problem since long in iOS platform. When the iOS app is running on screen, having foreground mode then the app can successfully receive a message which has been send by another user. When the app is running in the background mode and someone sends a message, I want to notify the user by using local notification (No Push remote notification - Because I think as my app is already running in minimized mode there is no need to wake up the app by implementing Push notification). Even I have implemented local notification successfully but the problem is,
When the iOS application heads to the background mode, the main thread (task) is paused so, when some user sends a message the app is not able to execute the code (when app is minimized) so that it won’t be able to show the local notification. But when the application is brought back to the foreground the thread/task get resumed and then ie shows up the local notification and also the message.
I already have selected the "Background fetch" property under Background Modes in Info.plist. I have also added below the line in my FinishedLaunching method
UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
I have already worked and implemented code from below links, but didn’t worked for me.
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks#performing-tasks-during-didenterbackground
Xamarin forms background tasks run only when app is open on ios
https://arteksoftware.com/backgrounding-with-xamarin-forms/
When an iOS application goes to the background, are lengthy tasks paused?
How to perform a simple background task on Xamarin iOS
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks#creating-background-safe-tasks
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/
I think my issue is relate to iOS Background processing, So, does anyone has idea what to do, to execute the code when app is already in minimized mode?
Generally speaking, backgrounding is very restricted on iOS. If your app is in a certain category (e.g. Navigation, Music) you'd get extended backgrounding capabilities, but I don't believe that chat apps do. More specifically, Background Fetch is not really suitable for your problem. It is called on an irregular basis to fetch contents to be cached within your app in order to make showing contents to your users faster. Background fetch intervals may vary from 15 min to several hours (not sure about the latter).
What you need is remote notifications.
Remote notifications (also known as push notifications) let you push small amounts of data to devices on which your app is installed, even when your app isn't running.
Remote notifications are brokered via a priviliged service (Apple Push Notification service - APNs) to Apple devices and delivered in a timely manner (seconds rather than minutes or hours). Usually you'd want to keep the payload as little as possible (just send the chat ID for example) and let the app fetch its data when it's notified.
Speaking in terms of a chat application, your chat server would send the remote notification to the APNs whenever a user sends a message to the chat. The app would be notified, fetch additional data and then display the notification to the user. If the notification is tapped, the user would be taken to the chat window for the respective chat.
Please note that your app has to be registered with APNs, otherwise remote notifications won't work.

Is there a way to keep CoreData in Sync with a Server API even when the App is in Background/Suspended/Terminated states?

I'm trying to make an App that's exactly like WhatsApp, so far the biggest limitation is that when a user re-opens the App after a while, and goes to a Conversation View, new Messages that were sent during the time the App was in Background/Suspended or Terminated states have to be recovered from a Server (API), and this causes a potential delay for new messages showing up in the Conversation View.
How can I achieve permanent Sync of an App with a server API? I'm kind of sure apps like WhatsApp manage to do this, because Messages never appear to be downloaded whenever you re-open the App after a while, right?
What is the right way to sort of Mimic this type of behavior? I really want to learn how to perform this kind of "advanced" synching and I don't know where to get started. Again, my goal is to try to avoid server-downloads of new Messages when a user re-enters a Conversation Viewfor example (because that would be bad UX)
Thanks in advance!
Your app will need push notifications. This is a must have for apps like this.
On the server, when you know that there are new messages, send a push notification to the app. This will let the app know that there are messages to read. At this point the app can badge the app icon, pop an alert and show on the notification screen. The user controls how notifications appear.
Details about notifications here:
https://developer.apple.com/notifications/
Your app can load all messages when it gets the notification.
iOS apps do have some limited options for background processes: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

how it works whatsapp

How does Whatsapp continue to receive notifications of received messages, even after you kill the app or restart the phone?
I am using an application that sends location coordinates, and it's for iOS, written in Swift.
My problem is that when I kill the app, the application stops sending coordinates. How I can make it restart automatically? For example, Whastapp, despite killing the app, if someone sends me a message, I receive the notification
I have also tried creating a widget app, but this is activated only when under the bar where the widget exists.
WhatsApp continues receiving PushNotifications. These are sent by Web Server and the App need not be active for it.
Coming to your requirement, you can use options like requestAlwaysAuthorization (to keep App awake in background to be able to send notifications). SignificantLocationChange, RegionMonitoringService, VisitService (to relaunch you app in the background if terminated).
Hope it helps.
Use this tutorial https://www.raywenderlich.com/123862/push-notifications-tutorial. you need to have one developer account to use push notification. It's easy to implement.

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 display message on lockscreen

One of our clients asked for this functionality: "Permanently display a message (or image) on the iPhone lockscreen".
Our initial ideas were:
Changing the device lockscreen image:
Couldn't find a solution for doing this. Couldn't find even a private API for it.
Playing an audio in background and display an album artwork. It has a few problems:
We cannot hide the volume and track buttons
Display a notification:
The notification will go away after the user skips the lockscreen
Any ideea how we can accomplish this request. Please note that the app is NOT intended to be distributed via AppStore. This is an internal app only. It should work on NON-Jailbreak devices through. The app will be enterprise distribuited.
Kind regards,
Artwork
If it's not intended for the app store, you could definitely use the artwork thing. Since you don't need to comply with the iOS guidelines, just warn your users that the lockscreen buttons won't work if you try using them. This link and this one should help.
Using the iOS 7 background fetch can help you manage messages display, as long as the app has already been opened once. To know if the app is opened, you can send ping to your server saving the current app state every X minutes. If the server doesn't receive an update, it will just guess that the app is not running, he'll send emails or push notifications to the user until he does so.
Issues:
If the user opens an app like Music or any other that can become a responder, it will override your message.
You can only display a message if the app is opened at least once.
Remote notifications
It could be a good option since a notification can stay on the lockscreen as long as the user or you doesn't clear the notification center (you have to set it). And you can remotely display any message without the app being launched.
Issues:
You have no way of knowing if he cleared the notification. An idea would be to send them an email if notifications have been disabled.
Users can just disable notifications.
Lockscreen image
You won't be able to change the device lockscreen image without a jailbroken device.
I'd go with the first one, if it's a client requirement it seems like a reliable one. It all depends on how much control they have on the devices.

Resources