Reputation: 930
So our app makes use of UILocalNotifications which are scheduled for every morning at 9am. The content of this message is dependant on an API call which I want to make 5 minutes before the notification is scheduled for.
So for example, the notification might be set to remind the user to do task x, but there is the chance that the user has already task x (via the online portal and not app), in which case we want to tell them to do task Y instead. For various reasons we don't have a push notification server set up yet but will soon, so this is an interim solution fo rhte purpose of testing.
So my question is: How can I schedule an API call to be made which replaces the immiment notification message depending on the response, even if the app is in the background, or even closed?
Thanks!
Upvotes: 2
Views: 1701
Reputation: 9898
You have to use silent push notification. in payload you can have get various information from server. then schedule UILocalNotification
at 9:00 AM.
When you are using silent push notification your app will invoke in background / terminated state ( Note - would not come foreground ) upto your UILocalNotification
sound file plays ( max 30 seconds ), in this 30 seconds you can do API related work.
When UILocalNotification
display in notification center, For example there are 2 buttons with notification "Accepted task" and "Later". So when you tap on though buttons even your app will invoke in background / terminated state, even you can open app and redirect to specific view controller. with both case you can do API related work.
If you keep your UILocalNotification
object in NSUserDefault
then you can also retrieve it on didFinishLaunchingWithOption
, in case your device is restarted and UILocalNotification
and further API work is very crucial.
Let me know you need any help for silent push notification, UILocalNotification
, didFinishLaunchingWithOption
or anything.
Referral note
https://www.raywenderlich.com/123862/push-notifications-tutorial
https://www.sinch.com/tutorials/ios8-apps-and-pushkit/
https://developer.apple.com/reference/pushkit
Source https://github.com/hasyapanchasara/PushKit_SilentPushNotification
Push kit implementation code
import UIKit import PushKit
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
self. PushKitRegistration()
return true
}
//MARK: - PushKitRegistration
func PushKitRegistration()
{
let mainQueue = dispatch_get_main_queue()
// Create a push registry object
if #available(iOS 8.0, *) {
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
} else {
// Fallback on earlier versions
}
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// Register VoIP push token (a property of PKPushCredentials) with server
let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
print(hexString)
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
// Process the received push
// From here you can schedule UILocalNotification
}
}
Upvotes: 1