iOS

2015-06-13 13 views
11

'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!

+3

Evet, sadece uygun zamanda yürütülecek bu kodu taşıyabilirsiniz :) – SomeGuy

+0

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

+2

Uygulamayı nasıl edinirsiniz: UIApplication başka bir viewController içine mi? – denislexic

cevap

4

Cevap basittir. Kullanıcının başka bir zamanda istenmesini isterseniz, örneğin bir tuşa basıldığında, istekle ilgili kodu yalnızca bu işleve taşıyın (veya başka bir yerden promptUserToRegisterPushNotifications() numaralı telefonu arayın).

Umut yardımcı olur :) Bu benim MainViewController.swift içinde promptUserToRegisterPushNotifications() yerleştirdik, ama aynı yerleştirin zaman işe yaramadı çünkü AppDelegate içinde didRegisterForRemoteNotificationsWithDeviceToken bırakmış Swift 2. içindir

+1

Teşekkürler Linus. :) Şu an bunu denemek için bilgisayarımdan uzaktayım ve varsaydığım şey buydu, bu yüzden açıklama için teşekkürler. :) bunu deneyecek ve geri bildirim ile rapor edecek! Teşekkürler! – Simon

+0

Merhaba Linus, inanın ya da inanmayın, Sadece şu anda bu çözümü uygulamaya geliyorum. Bu yüzden, istediğiniz komut istemini "promptUserToRegisterPushNotifications()" yöntemini farklı bir görünüm denetleyicisine taşıdım, ancak şimdi yukarıda Denislexic olarak ortaya çıkmış olmama neden oluyorsunuz. "Uygulamayı: UIApplication" konusuna nasıl başvurabilirim? MainViewController.swift dosyam nedir? – Simon

+0

Sorun değil;) Ahm neden bütün 'promptUserToRegisterPushNotifications()' yöntemini MainVC'nize yerleştirmiyorsunuz? – LinusGeffarth

2

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

Bu benim kod yazdım yöntemdir ve denir kez başlatılması üzerine çalışıyor (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() 
    } 
}