Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.84 KB | None | 0 0
  1. //
  2. //  AppDelegate.swift
  3. //  PushPizzaDemo
  4. //
  5. //  Created by Steven Lipton on 1/2/17.
  6. //  Copyright © 2017 Steven Lipton. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import UserNotifications
  11.  
  12. @UIApplicationMain
  13. class AppDelegate: UIResponder, UIApplicationDelegate {
  14.  
  15.     var window: UIWindow?
  16.  
  17. // Check if you have permission to use notifications.
  18.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  19.         // Override point for customization after application launch.
  20.         UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge])
  21.         {(granted,error) in
  22.             if granted{
  23.                 application.registerForRemoteNotifications()
  24.             } else {
  25.                 print("User Notification permission denied: \(error?.localizedDescription)")
  26.             }
  27.              
  28.         }
  29.         return true
  30.     }
  31.          
  32.  //code to make a token string  
  33.     func tokenString(_ deviceToken:Data) -> String{
  34.         let bytes = [UInt8](deviceToken)
  35.         var token = ""
  36.         for byte in bytes{
  37.             token += String(format: "%02x",byte)
  38.         }
  39.         return token
  40.     }
  41. // Successful registration and you have a token. Send the token to your provider, in this case the console for cut and paste.
  42.     func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  43.        
  44.         print("Successful registration. Token is:")
  45.         print(tokenString(deviceToken))
  46.     }
  47. // Failed registration. Explain why.    
  48.     func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  49.         print("Failed to register for remote notifications: \(error.localizedDescription)")
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement