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!
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
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
Làm thế nào để bạn có được ứng dụng: UIApplication vào một viewController khác? – denislexic