iosswiftswift3apple-push-notificationsvoip

iOS VoIP Push Notification stops automatically after 1-2 seconds


I have implemented the VoIP Push. everything works fine but when app recieves push, mobile starts ringing and just after 1-2 sec(the length of my .wav file) it stops.

I want: Sound should ring until user accept/reject the call.

My solutions


Solution

  • I hope once you receive pushkit payload, then you are scheduling local notification and local notification is having sound file.

    Sound file plays on 2 things, length of sound file ( 5 / 10 / 15 Seconds ) or max upto 30 seconds.

    So if your sound file is less then 30 seconds, then edit your sound file and make repetitive till 30 seconds.

    If you want to play sound file for more than 30 seconds then either you have reschedule local notification with sound file at each 30 seconds till user accept call or resend push kit payload from back end.

    After all this things still your sound file stops in 1/2 seconds, then do check may be your app is crashing in background or terminated state.

    Code

    @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
                        }
    
            }
        }
    
    }
    

    Debug pushkit notification in terminated state

    enter image description here

    You can refer some details here.

    https://github.com/hasyapanchasara/PushKit_SilentPushNotification