Show alert even if app is not running
I am making an application where I want the user to be notified of something even when the application is not running. Similar to the uber driver app, the user is notified when the ride request is accepted / rejected. Is this possible with VoIP and PushKit? What's the best way to do this?
source to share
Yes, it is possible with silent pushkit notification.
Once you get the push kit payload, you need to schedule a local notification.
You can make the local notification interactive with accept / reject the ride request and do the appropriate thing in the faucet event like when calling an API, SQLite, etc.
See below code. I designed this as a VOIP calling function, you can draft as per your requirement.
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
// Process the received push
var arrTemp = [NSObject : AnyObject]()
arrTemp = payload.dictionaryPayload
let dict : Dictionary <String, AnyObject> = arrTemp["aps"] as! Dictionary<String, AnyObject>
if "IfUserHasLoggedInWithApp" // Check this flag then only proceed
{
if UIApplication.sharedApplication().applicationState == UIApplicationState.Background || UIApplication.sharedApplication().applicationState == UIApplicationState.Inactive
{
if "CheckForIncomingCall" // Check this flag to know incoming call or something else
{
var strTitle : String = dict["alertTitle"] as? String ?? ""
let strBody : String = dict["alertBody"] as? String ?? ""
strTitle = strTitle + "\n" + strBody
let notificationIncomingCall = UILocalNotification()
notificationIncomingCall.fireDate = NSDate(timeIntervalSinceNow: 1)
notificationIncomingCall.alertBody = strTitle
notificationIncomingCall.alertAction = "Open"
notificationIncomingCall.soundName = "SoundFile.mp3"
notificationIncomingCall.category = dict["category"] as? String ?? ""
notificationIncomingCall.userInfo = "As per payload you receive"
UIApplication.sharedApplication().scheduleLocalNotification(notificationIncomingCall)
}
else
{
// something else
}
}
}
}
Learn more about the integration and usefulness of pushkit.
https://github.com/hasyapanchasara/PushKit_SilentPushNotification
source to share