'da push bildirimi izinleri için kullanıcıdan ne zaman istendiğini denetleme iPhone için Swift ve Xcode 6 kullanan bir uygulama geliştirdim ve hizmetleri işlemek için Ayrıştırma çerçevesi.iOS
Anımsatıcı bildirimleri, push bildirimlerinin nasıl ayarlanacağını takip ederken, yönergeler push bildirimlerini App Delegate dosyasına koymamı tavsiye etti.
Bu
Ben App Delege dosyasına eklemiş kod ...@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()
}
}
ne olur en kısa sürede uygulama ilk kez başlatıldığında olarak, kullanıcı izni istenir olmasıdır olduğunu bu izinler.
Yapmak istediğim şey, yalnızca belirli bir eylemin gerçekleşmesinden sonra (yani uygulamanın özelliklerinin izlenmesi sırasında) bu izinlerin sorulmasıdır, bu yüzden neden onları istediğimiz konusunda biraz daha fazla bağlam sağlayabilirim push bildirimlerine izin vermek için
Aşağıdaki kodu, yalnızca ViewController'da aşağıdaki kodu kopyalamak mümkün mü?
// 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()
}
Teşekkürler!
Evet, sadece uygun zamanda yürütülecek bu kodu taşıyabilirsiniz :) – SomeGuy
Teşekkürler SomeGuy! Projeme geri döndüğümde ve daha fazla yardıma ihtiyacım olursa rapor verdikten sonra deneyeceğim. Teşekkürler! – Simon
Uygulamayı nasıl edinirsiniz: UIApplication başka bir viewController içine mi? – denislexic