即使应用未运行,也会显示提醒

时间:2017-05-10 12:39:01

标签: ios swift push-notification voip pushkit

enter image description here我正在做一个应用程序,我希望用户在应用程序未运行时收到通知。就像在超级驱动程序应用程序中一样,用户被警告接受/拒绝乘车请求。 VoIP和PushKit有可能吗?这是最好的方式吗?

1 个答案:

答案 0 :(得分: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

enter image description here