I am building an app that has can be used by the same user across multiple devices. Whenever the user updates a preference on one device, I want the changes to propagate to all the devices with the app whether they are on the foreground or on the background. Can I achieve this using push notifications {aps: {content-available: 1}} payload?
Is there a restriction on which category of apps can use this?
Thanks
No there isn't a restriction on app category, you just have to implement the proper background mode in your xcode project.
You can use content-available for that, it's meant for when new information is available to the app and you want to update it locally before the user opens the app so it's more readily available. This is a perfect use case of that functionality.
From Apple:
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.
EDIT: One thing to note, don't make the preferences update only available through the push notification because they aren't 100% reliable. Make the info available to update upon opening the app as well so that you can be sure they got it.
Related
According to Apple's official documentation, if one sends a "silent notification", i.e. a notification with content-available: 1 we have
For background download apps: A push notification arrives for an app
and the payload of the notification contains the content-available key
with a value of 1. The system wakes the app at opportunistic moments
to begin downloading new content. For apps downloading content in the
background using the NSURLSession class, all tasks associated with
that session object either completed successfully or received an
error. A download initiated by a Newsstand app finishes.
also it says in that document
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.
How to interpret this? If an app is submitted with a UIBackgroundMode of remote-notification in the plist, does this mean that the app is actually launched in the background if we send a remote notification? Even if the user has restarted the iOS device, and didn't launch our app? Even if the user force-quit our app before?
My question is, how reliable is the above expectation? Has anyone had direct experience with and tested this, and it is true 100% of the time in your experience?
From Apple
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
iOS is gonna wake up your app and will give you up to 30 seconds to download whatever you need. Keep in mind that notifications are not reliable, so you can't trust they're gonna arrive 100% of the time. If you have really important data to show, you'd need to verify that your app is sync when user opens it. Background fetching is just to improve user experience
Display my push notification message is "Message from: +919687335565"
attached screenshot :
I just want to fetch the name of this +919687335565 contact number from the Address book and display name of this contact instead of a phone number in push notification.
Before displaying notification i want to replace phone number +919687335565 with its contact name Chandresh in push notification.
like attached screenshot :
There are various possibilities for preprocessing and modify the payload of a notification on iOS.
Before iOS 10
You could be sending Silent Notifications, what will not be not shown to the user. Will wake our application when it is terminated or in background, and you will be able to do preprocessing on the notification content. See more info here, how to set it up. However, this notification type is not 100% reliable, and should not be abused, e.g. used for all notifications to be delivered, because Apple could stop the notifications to be sent after a number of messages.
The other option is, if your application supports VoIP. This way your app will pretty much always listen to push notifications, and you will be always available to preprocess the notifications, before you would be displaying them. However, if your app does not have real VoIP capabilities, e.g. phone calls, your app will be rejected by Apple on the review. Here is a great tutorial, how to set it up.
From iOS 10
With the introduction of iOS 10, we are finally able to do preprocessing on our notifications, even, when the app is in background or terminated. No VoIP capabilities or special type of notifications needed to be sent. Here is a great tutorial, how to set it up.
This is possible, except:
want to fetch name of this +91********* contact number from Address book
because Address Book is not accessible when app is in background!
You have to write separate logic in app itself to replace mobile no by appropriate name.
How other than this is possible:
Before iOS 10 and iOS 8+:
Use PushKit to send silent push notifications, app will be woken up by iOS, change the mobile no by name, show local notification.
iOS 10+:
Use Notification Service Extension to alter notification content before it'll shown to user.
Look at this official documentation
Using this service extension, iOS gives you a way to first receive notification directly to app, modify content and then deliver to user.
Here you can change mobile no by name.
No this is not possible.
Because the push notification is handle by server.
This message is only change by server not from app because that time
app is in background or terminated.
But you can change message when app is in foreground and background fetch is
enable then you can customize your message and can show it.
I'm looking into reliable ways of updating an iOS app with content of critical importance, when instructed to do so by a server.
This would have to trigger regardless of the state of the app (foreground, background, not running, etc). Most sources seem to indicate that Push Notifications with the content-available: 1parameter are able of doing this.
My question is, what happens if the user either presses Cancel when prompted to allow the app to receive push notifications, or turns them off afterwards ? Is he only not going to receive alerts anymore, or will the push notifications be entirely ignored (including the content update) ? And if so, is there any other iOS feature that would reliably allow me to update the app's content (regardless of app state and without the user being able to disable it) ?
This is about iOS 7 and above.
If user disallows the push notifications , you can not send them the notifications forcefully.
What kind of data you want to update afterwards?
Most of the data can be changed dynamically by web services...
Can I somehow send a whole file with a push notification on iOS, or send a notification to a device to download a file from a server.
The device NEEDS to do that on it's OWN, without user interaction and I need it to work without updating the app in the store.
So is that possible? If not any advices/alternatives are welcome.
Can I somehow send a whole file with a push notification on iOS
While you could serialize your file and send it, push notifications are limited to 2kB.
or send a notification to a device to download a file from a server.
You can do it with "Remote notifications" background mode.
The device NEEDS to do that on it's OWN, without user interaction
The previous option will work, unless your user manually killed your app.
I need it to work without updating the app in the store.
You will need to update your app. At least to turn on "Remote notifications" background mode and handle the incoming data.
EDIT:
And don't forget that Push Notifications are NOT RELIABLE!
A Push notification can be delayed or even never distributed. That's why you should NEVER rely on it to achieve any critical work.
No, you cannot push an entire file given that a push notification is limited in size to 2KB (256 bytes in pre-iOS8). It is possible to cause an app to execute code using a push notificiation.
Depending on the type of app, it might be able to ocassionally poll your server and retrieve information. All in all, you will certainly have to make changes to your app's code, thereby requiring you to publish a new version on the App Store.
Can I somehow send a whole file with a push notification on iOS
NO
You can learn more about Push Notifications and get answers to other questions here
How we leverage iOS push notifications
I am writing an application that utilizes Apple's Push Notification Service. Some of the push notifications are based on the users location and should only be delivered if the user is a certain distance from an object. I don't want to continually update the user's position to my server and do the check that way, first, because of security reasons and second, to cut down on the network usage. Is there a way, when the push notification is received by the device, to do a check before the user is notified, and if it doesn't meet the criteria, discard the notification? Thanks for the help!
Nope, sadly you can't execute any code on the client side without the user clicking on the notification when the app is not launched. You'll have to do your check server-side to decide wether or not sending a push.
Push notifications that are received while you app is not running (the most typical case) is outside your control. Once they are sent, they will be received and shown to the user (assuming the user has granted permissions)
You do have control over push notifications if they are received while your app is running.
Maybe you could use local notifications (notifications that are generated and received from user's device) instead. You would have full control over when they are generated.
Bear in mind, background processing in apps is disallowed except for 4 things
Location tracking (You can subscribe to trigger code when user changes geolocation)
Alarms
Playing music
Voice over IP
Anything outside these cannot be executed in the background.
Starting with iOS7, your code can be run, I quote, "roughly the same time that the user sees the notification":
Multitasking Enhancements
Apps that use push notifications to notify the user that new content
is available can fetch the content in the background. To support this
mode, include the UIBackgroundModes key with the remote-notification
value in your app’s Info.plist file. You must also implement the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method in your app delegate.
See more info here:
https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW10
and here:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW57
I haven't tried it yet but I wonder if you send a push notification payload which doesn't have the standard "alert", "badge" values in "apns" but in a custom value, it would still call your code but without displaying the push notification.