How to set up push notifications in Swift

后端 未结 11 1180
生来不讨喜
生来不讨喜 2020-11-27 10:48

I am trying to set up a push notification system for my application. I have a server and a developer license to set up the push notification service.

I am currently

相关标签:
11条回答
  • 2020-11-27 11:44

    To support ios 8 and before, use this:

    // Register for Push Notitications, if running iOS 8
    if application.respondsToSelector("registerUserNotificationSettings:") {
    
      let types:UIUserNotificationType = (.Alert | .Badge | .Sound)
      let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
    
      application.registerUserNotificationSettings(settings)
      application.registerForRemoteNotifications()
    
    } else {      
      // Register for Push Notifications before iOS 8
      application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
    }
    
    0 讨论(0)
  • 2020-11-27 11:46

    I use this code snip in AppDelegate.swift:

    let pushType = UIUserNotificationType.alert.union(.badge).union(.sound)
    let pushSettings = UIUserNotificationSettings(types: pushType
                , categories: nil)
    
    application.registerUserNotificationSettings(pushSettings)
    application.registerForRemoteNotifications()
    
    0 讨论(0)
  • 2020-11-27 11:51

    Swift 2:

    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    UIApplication.sharedApplication().registerForRemoteNotifications()
    
    0 讨论(0)
  • 2020-11-27 11:52

    Swift 4

    Import the UserNotifications framework and add the UNUserNotificationCenterDelegate in AppDelegate

    import UserNotifications
    
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
    

    To Register Application for APNS, (Include the following code in didFinishLaunchingWithOptions method inside AppDelegate.swift)

    let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()
    

    This will call following delegate method

    func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //send this device token to server
    
    }
    
    //Called if unable to register for APNS.
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print(error)
    }
    

    On Receiving notification following delegate will call:

    private func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    
        print("Recived: \(userInfo)")
        //Parsing userinfo:
    
    }
    
    0 讨论(0)
  • 2020-11-27 11:55

    You can send notification using the following code snippet:

    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    if(UIApplication.sharedApplication().currentUserNotificationSettings() == settings ){
    //OK
    }else{
    //KO
    }
    
    0 讨论(0)
提交回复
热议问题