Search code examples
iosxcodevoipuiapplicationpushkit

How to replace UIRemoteNotificationTypeVoip method with setKeepAliveTimeout method?


In my XCode project, i have used setKeepAliveTimeout method in applicationDidEnterBackground method like below code.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [self performSelectorOnMainThread:@selector(keepAlive) withObject:nil waitUntilDone:YES];

    [application setKeepAliveTimeout:600 handler: ^{
    [self performSelectorOnMainThread:@selector(keepAlive) withObject:nil waitUntilDone:YES];
    }];
}

It shows that setKeepAliveTimeout method is deprecated and they wants to use UIRemoteNotificationTypeVoip method.

I searched for UIRemoteNotificationTypeVoip method, but not enough results are given. Even developer.apple.com doesn't have documentation for that method.

Problem: How to change UIRemoteNotificationTypeVoip where setKeepAliveTimeout is used?

If anyone knows, then give me an answer.

Thanks in Advance!


Solution

  • Use below structure to achieve your task.

    Some reference

    https://github.com/hasyapanchasara/PushKit_SilentPushNotification

    enter image description here

    enter image description here

    Use this simplepush.php file

    Use below commands to create pem file and use it in above code.

    After that go to simplepush.php location and fire command -> php simplepush.php

    This way you can test your push kit notification setup architecture.

    https://zeropush.com/guide/guide-to-pushkit-and-voip

    https://www.raywenderlich.com/123862/push-notifications-tutorial

    Download

    import UIKit
    import PushKit
    
    
    class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{
    
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
    
        let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
        application.registerForRemoteNotificationTypes(types)
    
        self. PushKitRegistration()
    
        return true
    }
    
    
    
    //MARK: - PushKitRegistration
    
    func PushKitRegistration()
    {
    
        let mainQueue = dispatch_get_main_queue()
        // Create a push registry object
        if #available(iOS 8.0, *) {
    
            let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
    
            // Set the registry's delegate to self
    
            voipRegistry.delegate = self
    
            // Set the push type to VoIP
    
            voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
    
        } else {
            // Fallback on earlier versions
        }
    
    
    }
    
    
    @available(iOS 8.0, *)
    func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
        // Register VoIP push token (a property of PKPushCredentials) with server
    
        let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
            count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
    
        print(hexString)
    
    
    }
    
    
    @available(iOS 8.0, *)
    func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
        // Process the received push
        // From here you have to schedule your local notification
    
    }
    
    }
    

    Through pushkit payload you can schedule local notification. when pushkit payload comes your app will active in background or terminated state upto your local notification sound plays. you can also keep details in NSUserDefault. so on interactive local notification or didfinishlaunching with option you can do whatever you want.