我正在做一个应用程序,即使该应用程序未运行,我也希望在其中通知用户。就像在uber driver应用中一样,系统会提醒用户接受/拒绝乘车请求。 VoIP和PushKit是否可能?哪种方法最好?
最佳答案
是的,这可以通过Pushkit静默通知来实现。
收到推包有效载荷后,您必须安排本地通知。
您可以使本地通知与接受/拒绝乘车请求进行交互,并在点击事件中执行适当的操作,例如API调用,SQLite等。
请参考以下代码。 我已经根据VOIP call 功能起草了此文件,您可以根据需要起草。
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
}
}
}
}
请参阅有关pushkit集成和有效负载的更多信息。
https://github.com/hasyapanchasara/PushKit_SilentPushNotification