Skip to end of metadata
Go to start of metadata

AppDelegate object calls the following APIs to invoke Push Notification Service:

// Register for Push Notifications
    PushNotificationService.sharedInstance().initialize(application, delegate:self)
        
       
    
    // App in foreground
    PushNotificationService.sharedInstance().applicationInForeground()
        
  
   
    // App in background
    PushNotificationService.sharedInstance().applicationInBackground()
     
    // Register the device with GCM
        PushNotificationService.sharedInstance().registerDevice(deviceToken)
    
    // APNS Token Error
   PushNotificationService.sharedInstance().registrationFailed(application, error:err)
   
  
    // Received notification
     PushNotificationService.sharedInstance().receivedNotification(userInfo)



   //Recived data Notification background
    PushNotificationService.sharedInstance().receivedNotificationBackground(userInfo, 
   
   // Token was refreshed
   PushNotificationService.sharedInstance().tokenRefresh();

 

The following is a sample code to invoke the Push Notification Service:

class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate, GCMReceiverDelegate {
    
    var window: UIWindow?
    
    let registrationKey = "onRegistrationCompleted"
    var viewController: LoginViewController?
    var centerContainer: MMDrawerController?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:
        [NSObject: AnyObject]?) -> Bool {
        // Register for Push Notifications
        PushNotificationService.sharedInstance().initialize(application, delegate:self)
        
        return true
    }
    
    // App in foreground
    func applicationDidBecomeActive( application: UIApplication) {
        PushNotificationService.sharedInstance().applicationInForeground()
        if (self.viewController != nil) {
            let client: SonusWRTCClient = self.viewController!.client
            print(client.isConnected(),"  client.isConnected()  " )
            if (client.isConnected() == true) {
                // Relogin the user
                print(" re login")
                self.viewController?.thirdPartyLogin()
            }else{
                
            }
        }
    }
   
    // App in background
    func applicationDidEnterBackground(application: UIApplication) {
        PushNotificationService.sharedInstance().applicationInBackground()
        if (self.viewController != nil) {
            let client: SonusWRTCClient = self.viewController!.client
            if (client.isConnected()) {
                // Relogin the user
                self.viewController?.logout()
            }
        }
    }
    
    // Received APNS Token
    func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken
        deviceToken: NSData ) {
        // Register the device with GCM
        PushNotificationService.sharedInstance().registerDevice(deviceToken)
    }
    
    // APNS Token Error
    func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
        err: NSError ) {
        PushNotificationService.sharedInstance().registrationFailed(application, error:err)
    }
  
    // Received notification
    func application( application: UIApplication,
                      didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        print(" Recived data Notification")
        PushNotificationService.sharedInstance().receivedNotification(userInfo)
    }
    
    // Received notification in background
    func application( application: UIApplication,
                      didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                                                   fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
        print(" Recived data Notification background")
        PushNotificationService.sharedInstance().receivedNotificationBackground(userInfo, fetchCompletionHandler: handler)
    }
    
    // Token refresh
    func onTokenRefresh() {
        // Token was refreshed
        PushNotificationService.sharedInstance().tokenRefresh();
    }
    
    func willSendDataMessageWithID(messageID: String!, error: NSError!) {
        if (error != nil) {
            // Failed to send the message.
        } else {
            // Will send message, you can save the messageID to track the message
        }
    }
    
    func didSendDataMessageWithID(messageID: String!) {
        // Did successfully send message identified by messageID
    }
        
    func didDeleteMessagesOnServer() {
        // Some messages sent to this device were deleted on the GCM server before reception, likely
        // because the TTL expired. The client should notify the app server of this, so that the app
        // server can resend those messages.
    }
    
    
    
       
}

 
  • No labels