iOS VoIP 推送通知在 1-2 秒后自动停止

iOS VoIP Push Notification stops automatically after 1-2 seconds

我已经实现了 VoIP 推送。一切正常,但是当应用程序收到推送时,手机开始响铃,并在 1-2 秒(我的 .wav 文件的长度)后停止。

我想要: 声音应该响到用户 accept/reject 呼叫。

我的解决方案

我希望一旦你收到 pushkit payload,那么你正在安排本地通知并且本地通知有声音文件。

声音文件在 2 件事上播放,声音文件的长度(5 / 10 / 15 秒)或最长 30 秒。

因此,如果您的声音文件少于 30 秒,请编辑您的声音文件并重复播放直到 30 秒。

如果您想播放超过 30 秒的声音文件,那么您可以重新安排每 30 秒一次使用声音文件进行本地通知,直到用户接听电话或从后端重新发送推送工具包负载。

在这一切之后你的声音文件仍然在 1/2 秒内停止,然后检查你的应用程序是否在后台崩溃或终止状态。

代码

@available(iOS 8.0, *)
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 通知

  • 将调试指针放在委托方法上
  • 去编辑方案
  • Select 运行 选项然后启动 -> 等待可执行文件启动
  • 从后端发送推送套件负载
  • 在设备上获得负载后
  • 它将自动调用并且调试指针将在委托方法中调用。

你可以在这里参考一些细节。

https://github.com/hasyapanchasara/PushKit_SilentPushNotification