即使应用不是 运行 也显示提醒

Show an alert even when the app is not running

我正在开发一个应用程序,我希望即使该应用程序不是 运行,用户也能收到通知。就像在优步 driver 应用程序中一样,用户会收到 accept/Reject 乘车请求的提醒。 VoIP 和 PushKit 可以实现吗?哪种方法最好?

是的,这可以通过 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集成和payload的内容。

https://github.com/hasyapanchasara/PushKit_SilentPushNotification