Advertisement
thieumao

Test Push Noti

Oct 27th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.44 KB | None | 0 0
  1. //
  2. //  AppDelegate.swift
  3. //  testpushswift
  4. //
  5. //  Created by Nguyen Van Thieu B on 10/27/16.
  6. //  Copyright © 2016 Thieu Mao. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. @UIApplicationMain
  12. class AppDelegate: UIResponder, UIApplicationDelegate {
  13.  
  14.     var window: UIWindow?
  15.  
  16.  
  17.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  18.         registerForPushNotifications(application)
  19.         // Override point for customization after application launch.
  20.         return true
  21.     }
  22.  
  23.     func applicationWillResignActive(_ application: UIApplication) {
  24.         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  25.         // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  26.     }
  27.  
  28.     func applicationDidEnterBackground(_ application: UIApplication) {
  29.         // 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.
  30.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  31.     }
  32.  
  33.     func applicationWillEnterForeground(_ application: UIApplication) {
  34.         // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  35.     }
  36.  
  37.     func applicationDidBecomeActive(_ application: UIApplication) {
  38.         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  39.     }
  40.  
  41.     func applicationWillTerminate(_ application: UIApplication) {
  42.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  43.     }
  44.    
  45.     // MARK: Push Notification
  46.    
  47.     func registerForPushNotifications(_ application: UIApplication) {
  48.         let notificationSettings = UIUserNotificationSettings(
  49.             types: [.badge, .sound, .alert], categories: nil)
  50.         application.registerUserNotificationSettings(notificationSettings)
  51.     }
  52.    
  53.     func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
  54.         if notificationSettings.types != UIUserNotificationType() {
  55.             application.registerForRemoteNotifications()
  56.         }
  57.     }
  58.    
  59.     func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  60.         let tokenChars = (deviceToken as NSData).bytes.bindMemory(to: CChar.self, capacity: deviceToken.count)
  61.         var tokenString = ""
  62.        
  63.         for i in 0..<deviceToken.count {
  64.             tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
  65.         }
  66.        
  67.         print("Device Token:", tokenString)
  68.     }
  69.    
  70.     func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  71.         print("Failed to register:", error)
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement