ios - 即使应用程序未运行也显示警报

标签 ios swift push-notification voip pushkit

enter image description here我正在做一个应用程序,我希望即使应用程序未运行,用户也能收到通知。就像在优步司机应用程序中一样,用户会收到接受/拒绝乘车请求的提醒。 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

enter image description here

关于ios - 即使应用程序未运行也显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43892778/

相关文章:

ios - UITableView:在顶部添加单元格时保持选择(单选)?

c# - Plugin.MediaManager.Forms:无法从Xamarin.Forms应用程序数据目录播放视频

ios - ScrollView 仅显示最后添加的 subview

IOS应用加密

php - 尝试将数据发送到 GCM 时出现 MissingRegistraton 错误

ios - 在 UNNotificationServiceExtension 中执行 HTTP 请求

Android 相当于 Swift 的 HKObserverQuery?

ios - 在Swift中将十六进制整数存储在字节数组中(保留十六进制格式)

ios - Swift 3 中的 CoreData NSFetchRequest 错误 NSInvalidArgumentException

ios - 解析推送通知添加新操作