2015-06-13 17 views
11

Tôi đã tạo ứng dụng cho iPhone bằng Swift và Xcode 6 và khung công tác Parse để xử lý dịch vụ.Cách kiểm soát thời điểm nhắc người dùng cho phép thông báo đẩy trong iOS

Trong khi làm theo hướng dẫn Parse về cách thiết lập thông báo đẩy, hướng dẫn được khuyên rằng tôi đặt thông báo đẩy trong tệp App Delegate.

Đây là mã mà tôi đã thêm vào tập tin Đại biểu App ...

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    var pushNotificationsController: PushNotificationController? 


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     // Register for Push Notifications 
     self.pushNotificationsController = PushNotificationController() 

     if application.respondsToSelector("registerUserNotificationSettings:") { 
      println("registerUserNotificationSettings.RegisterForRemoteNotificatios") 
      let userNotificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound) 
      let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil) 
      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
     } 

     return true; 
    } 

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     println("didRegisterForRemoteNotificationsWithDeviceToken") 
     let installation = PFInstallation.currentInstallation() 
     installation.setDeviceTokenFromData(deviceToken) 
     installation.saveInBackground() 
    } 
} 

Vì vậy, những gì xảy ra là ngay sau khi ứng dụng được khởi động lần đầu tiên, người dùng sẽ được nhắc nhở để cấp các quyền này. Những gì tôi muốn làm, chỉ nhắc nhở các điều khoản này sau khi một hành động nào đó đã diễn ra (ví dụ, trong quá trình hướng dẫn các tính năng của ứng dụng) để tôi có thể cung cấp thêm một chút ngữ cảnh về lý do chúng tôi muốn chúng để cho phép thông báo đẩy.

Đơn giản là chỉ sao chép mã bên dưới trong ViewController có liên quan nơi tôi sẽ mong đợi nhắc người dùng?

// In 'MainViewController.swift' file 

func promptUserToRegisterPushNotifications() { 
     // Register for Push Notifications 
     self.pushNotificationsController = PushNotificationController() 

     if application.respondsToSelector("registerUserNotificationSettings:") { 
      println("registerUserNotificationSettings.RegisterForRemoteNotificatios") 
      let userNotificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound) 
      let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil) 
      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
     } 
} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     println("didRegisterForRemoteNotificationsWithDeviceToken") 
     let installation = PFInstallation.currentInstallation() 
     installation.setDeviceTokenFromData(deviceToken) 
     installation.saveInBackground() 
} 

cảm ơn!

+3

Yep, bạn chỉ có thể di chuyển mã đó được thực hiện tại thời điểm thích hợp :) – SomeGuy

+0

cảm ơn SomeGuy! Sẽ thử ngay sau khi tôi quay lại dự án của tôi và báo cáo lại nếu tôi cần thêm bất kỳ sự trợ giúp nào. Cảm ơn! – Simon

+2

Làm thế nào để bạn có được ứng dụng: UIApplication vào một viewController khác? – denislexic

Trả lời

4

Câu trả lời rất đơn giản. Nếu bạn muốn người dùng được nhắc một số thời gian khác, ví dụ trên một nút bấm thì chỉ cần di chuyển mã liên quan đến yêu cầu vào hàm đó (hoặc gọi promptUserToRegisterPushNotifications() từ một nơi khác).

Hy vọng rằng sẽ giúp :)

+1

Cảm ơn Linus. :) Tôi đang đi máy tính của tôi tại thời điểm này để thử này, và nó là những gì tôi giả định, vì vậy cảm ơn cho làm rõ. :) sẽ thử và báo cáo với phản hồi! Cảm ơn! – Simon

+0

Xin chào Linus, tin hay không, Im chỉ đến triển khai giải pháp này ngay bây giờ. Vì vậy, tôi đã làm như bạn đề nghị và di chuyển 'promptUserToRegisterPushNotifications()' phương pháp để một bộ điều khiển xem khác nhau, nhưng bây giờ Im nhận được vấn đề như Denislexic ở trên đưa lên .. làm thế nào để tôi tham khảo 'ứng dụng: UIApplication' để lưu các thiết lập vào tập tin MainViewController.swift của tôi? – Simon

+0

Không có vấn đề;) Ahm tại sao bạn không đặt toàn bộ 'promptUserToRegisterPushNotifications()' phương pháp vào MainVC của bạn? – LinusGeffarth

2

này là dành cho Swift 2. Tôi đã đặt promptUserToRegisterPushNotifications() trong MainViewController.swift, nhưng tôi đã rời didRegisterForRemoteNotificationsWithDeviceToken trong appdelegate bởi vì nó không hoạt động khi tôi đặt nó trên cùng một MainViewController.swift.

// In 'MainViewController.swift' file 
func promptUserToRegisterPushNotifications() { 
    // Register for Push Notifications 

    let application: UIApplication = UIApplication.sharedApplication() 

    if application.respondsToSelector(#selector(UIApplication.registerUserNotificationSettings(_:))) { 
     print("registerUserNotificationSettings.RegisterForRemoteNotificatios") 

     let notificationSettings = UIUserNotificationSettings(
      forTypes: [.Badge, .Sound, .Alert], categories: nil) 
     application.registerUserNotificationSettings(notificationSettings) // Register for Remote Push Notifications 
     application.registerForRemoteNotifications() 
    } 
} 


// In AppDelegate 
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes) 
    var tokenString = "" 

    for i in 0..<deviceToken.length { 
     tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]]) 
    } 

    NSUserDefaults.standardUserDefaults().setObject(tokenString, forKey: "deviceToken") 

    print("Device Token:", tokenString) 

} 
1

Đây là phương pháp tôi đã viết trong các mã và hoạt động tốt khi nó được gọi là ngày khởi động (didFinishLaunch)

class func registerNotification() { 
    if #available(iOS 10.0, *) { 
     // push notifications 
     UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .alert, .badge]) { 
      (granted, error) in 
      if (granted) { 
       UIApplication.shared.registerForRemoteNotifications() 
      } 
     } 

     let center = UNUserNotificationCenter.current() 
     center.delegate = AppManager.appDel() 
     center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in 
      if error == nil { 
       UIApplication.shared.registerForRemoteNotifications() 
      } 
     } 
    } else { 
     UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 
     UIApplication.shared.registerForRemoteNotifications() 
    } 
} 
Các vấn đề liên quan