I got a question that i can't figure out a long time. I know that i can't sync my application in "not-running" state. But let me show a simple scenario:
I killed WhatsApp. It's not running.
I send a message from another device.
Remote notification received and alert shown.
I activated "plane mode" on receiver device.
Then i launch WhatsApp.
It can't sync messages because of "plane mode" but i saw new message that received with remote notification.
I tested this scenario on iOS 9 and iOS 10 devices. How does it posible? Can anyone explain this?
I think they are using Remote Notifications background mode.
"If your server sends push notifications to a user’s device when new content is available for your app, you can ask the system to run your app in the background so that it can begin downloading the new content right away. The intent of this background mode is to minimize the amount of time that elapses between when a user sees a push notification and when your app is able to able to display the associated content. Apps are typically woken up at roughly the same time that the user sees the notification but that still gives you more time than you might have otherwise."
This is from developer apple. You can read more detailed about it in this artice under "Using Push Notifications to Initiate a Download".
Hope it will help.
Related
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.
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.
It is widely known that:
app doesn't receive push notification if it is in background or offline mode (app gets it once after user's action: tap on notification or app icon).
Apple push Notification service keep only ONE last notification when device is offline. Once device is connected to internet, APNs sends the last notification.
How to solve this?
very latest notification that just reached the app (not device) must reflect the actual number of notifications that are not implemented in the app yet. So, then I can download from the server last n notifications and implement them in the app at any time.
The question is:
How the server knows what notifications were implemented in the app, and which one not?
Notifications must be per device. Why? For instance, notification "remove object from Core Data" must be implemented in every device. Because only one user can be logged in on multiply devices at time.
You should track the state of the task (delete record or whatever your app needs to do) on the server and have the client report back when the task is done. Then flag the task as done.
Don't use push notifications as a reliable delivery method for your tasks, you will fail. Use the notifications as complementary part of your setup.
So for example when your app receives a notification, it can sync with the backend, to retrieve the tasks flagged as not done, execute them and then let the backend know that it's done.
I basically want to make multiple push notifications in the same application visible in the notification tray in iOS.
This scenario works if my data is on while push notification is triggered via APNS, but only the latest one is received in case I am offline and come back after a while. This functionality is affirmed by APNS documentaion.
However, this is what worked in WhatsApp:
Turned Data Connection OFF
Sent some messages to WhatsApp
Turned Data Connection ON
Saw multiple push notifications received in Apple's Notification Tray
How's this scenario working? Can I use APNS for this? If yes, then how?
See this sample image of multiple Push Notifications in WeChat.
Like you wrote in your question, this is mentioned in the Apple Docs:
If you are sending multiple notifications to the same device or
computer within a short period of time, the push service will send
only the last one.
Link
The only scenario that what you're describing will work is if your whatsApp was open in the background while getting those push notifications. That way whatsApp will handle them as local notifications and will present all of them in the notification center. If whatsApp was closed you'd get only the last notification like any other app.
You can easily test this:
Terminate whatsApp and turn on Airplane mode.
Send your device 5 messages from 1 to 5.
Turn Airplane mode off and lock your device.
You'll only see one msg (the last one you sent aka "5") in your notifications center.
This is how whatsApp is making it work:
While whatsApp is in the background, a single push notification is received (the last one the user sent, "5" in our example). That msg will not be shown to the user.
whatsApp receives it in the method application:didReceiveRemoteNotification:fetchCompletionHandler: and checks against their servers if there are any notifications prior to "5" that the user didn't receive. If that's the case, they will pull that data from their servers and will present it to the user using local notifications which is basically just a way to present data and not related to APNS at all.
It is explained in Troubleshooting Push Notifications. Check for "Some Notifications Received, but Not All" section.
As described you cannot have any control over those push notifications.
However you may know that from iOS7 a new background execution mode (remote-notification) allows the App to be awaken by the system when a push is received, allowing you to process some data, then go back to sleep...
This is probably the trick: using that way to receive the push notifications (silently) and then trigger your own local notification instead as #Segev said. See the UIBackgroundModes here.
I am new to iOS development and I recently came to know about the Apple Push Notification Service.
Does anyone know whether it is possible to push application updates to iPhone/iPad devices using Apple Push Notification Service..?
Thanks.
From the documentation:
Local notifications and push notifications are ways for an application
that isn’t running in the foreground to let its users know it has
information for them. The information could be a message, an impending
calendar event, or new data on a remote server. When presented by the
operating system, local and push notifications look and sound the
same. They can display an alert message or they can badge the
application icon. They can also play a sound when the alert or badge
number is shown.
So no, you can't push application updates. You could send a message telling the user that an update was available, though that would be considered pretty obnoxious by many users.