Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- // For debugging
- //OneSignal.setLogLevel(.LL_VERBOSE, visualLevel: .LL_NONE)
- //Enable all notification type.
- let notificationSettings = UIUserNotificationSettings(types: [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound] , categories: nil)
- //register the notification settings
- application.registerUserNotificationSettings(notificationSettings)
- return true
- }
- extension AppDelegate {
- func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
- //register for voip notifications
- let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
- voipRegistry.desiredPushTypes = Set([PKPushType.voIP])
- voipRegistry.delegate = self;
- print("didRegisterUserNotificationSettings")
- }
- }
- extension AppDelegate: PKPushRegistryDelegate {
- func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, forType type: PKPushType) {
- print("voip token: (pushCredentials.token)")
- }
- func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
- let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String>
- let message = payloadDict?["alert"]
- //present a local notifcation to visually see when we are recieving a VoIP Notification
- if UIApplication.shared.applicationState == UIApplicationState.background {
- let localNotification = UILocalNotification();
- localNotification.alertBody = message
- localNotification.applicationIconBadgeNumber = 1;
- localNotification.soundName = UILocalNotificationDefaultSoundName;
- UIApplication.shared.presentLocalNotificationNow(localNotification);
- }
- else {
- print(message)
- // dispatch_async(DispatchQueue.main, { () -> Void in
- //
- // let alert = UIAlertView(title: "VoIP Notification", message: message, delegate: nil, cancelButtonTitle: "Ok");
- // alert.show()
- // })
- }
- NSLog("incoming voip notfication: (payload.dictionaryPayload)")
- }
- func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenForType type: PKPushType) {
- NSLog("token invalidated")
- }
- }
- extension UIApplicationState {
- //help to output a string instead of an enum number
- var stringValue : String {
- get {
- switch(self) {
- case .active:
- return "Active"
- case .inactive:
- return "Inactive"
- case .background:
- return "Background"
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement