Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  2.  
  3. // For debugging
  4. //OneSignal.setLogLevel(.LL_VERBOSE, visualLevel: .LL_NONE)
  5.  
  6. //Enable all notification type.
  7. let notificationSettings = UIUserNotificationSettings(types: [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound] , categories: nil)
  8.  
  9. //register the notification settings
  10. application.registerUserNotificationSettings(notificationSettings)
  11.  
  12.  
  13. return true
  14. }
  15.  
  16. extension AppDelegate {
  17.  
  18. func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
  19.  
  20. //register for voip notifications
  21. let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
  22. voipRegistry.desiredPushTypes = Set([PKPushType.voIP])
  23. voipRegistry.delegate = self;
  24.  
  25. print("didRegisterUserNotificationSettings")
  26. }
  27. }
  28.  
  29.  
  30. extension AppDelegate: PKPushRegistryDelegate {
  31. func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, forType type: PKPushType) {
  32. print("voip token: (pushCredentials.token)")
  33.  
  34. }
  35. func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
  36.  
  37. let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String>
  38. let message = payloadDict?["alert"]
  39.  
  40. //present a local notifcation to visually see when we are recieving a VoIP Notification
  41. if UIApplication.shared.applicationState == UIApplicationState.background {
  42.  
  43. let localNotification = UILocalNotification();
  44. localNotification.alertBody = message
  45. localNotification.applicationIconBadgeNumber = 1;
  46. localNotification.soundName = UILocalNotificationDefaultSoundName;
  47.  
  48. UIApplication.shared.presentLocalNotificationNow(localNotification);
  49. }
  50.  
  51. else {
  52. print(message)
  53. // dispatch_async(DispatchQueue.main, { () -> Void in
  54. //
  55. // let alert = UIAlertView(title: "VoIP Notification", message: message, delegate: nil, cancelButtonTitle: "Ok");
  56. // alert.show()
  57. // })
  58. }
  59.  
  60. NSLog("incoming voip notfication: (payload.dictionaryPayload)")
  61. }
  62.  
  63. func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenForType type: PKPushType) {
  64. NSLog("token invalidated")
  65. }
  66. }
  67.  
  68. extension UIApplicationState {
  69.  
  70. //help to output a string instead of an enum number
  71. var stringValue : String {
  72. get {
  73. switch(self) {
  74. case .active:
  75. return "Active"
  76. case .inactive:
  77. return "Inactive"
  78. case .background:
  79. return "Background"
  80. }
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement