我正在做一个应用程序,即使应用程序未运行,我也希望在其中通知用户某些事情。就像在优步司机应用程序中一样,系统会提醒用户接受/拒绝乘车请求。VoIP 和 PushKit 可以做到这一点吗?最好的方法是什么?
问问题
358 次
1 回答
3
是的,这可以通过 pushkit 静默通知实现。
收到推送工具包有效负载后,您必须安排本地通知。
您可以使本地通知与接受/拒绝乘车请求交互,并在点击事件上做适当的事情,如 API 调用、SQLite 等。
参考下面的代码。我已经根据 VOIP 通话功能起草了这个,您可以根据您的要求起草。
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
于 2017-05-10T13:16:44.047 回答