2017-03-09 21 views
11

का उपयोग करके स्थानीय और पुश अधिसूचनाएं iOS 10 में विकसित हुईं। यह पूरी तरह से काम कर रहा है। लेकिन अब मुझे local notifications और push notification कोड कैसे करना चाहिए यदि उपयोगकर्ता iOS 9 और ऊपर के संस्करणों का उपयोग कर रहा है। क्या कोई मदद कर सकता है?आईओएस 9 और 10 में स्विफ्ट 3

नीचे iOS 10

import UIKit 
import UserNotifications 

@available(iOS 10.0, *) 
class ViewController: UIViewController,UNUserNotificationCenterDelegate { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     if #available(iOS 10.0, *) { 
     //Seeking permission of the user to display app notifications 
     UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: {didAllow,Error in }) 
     UNUserNotificationCenter.current().delegate = self 

     } 
    } 

    //To display notifications when app is running inforeground 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     completionHandler([.alert, .sound, .badge]) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func buttonPressed(_ sender: UIButton) { 

     if #available(iOS 10.0, *) { 

      //Setting content of the notification 
      let content = UNMutableNotificationContent() 
      content.title = "hello" 
      content.body = "notification pooped out" 
      content.badge = 1 

      //Setting time for notification trigger 
      let date = Date(timeIntervalSinceNow: 10) 
      var dateCompenents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date) 

      let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false) 
      //Adding Request 
      let request = UNNotificationRequest(identifier: "timerdone", content: content, trigger: trigger) 
      UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
     } 

    } 

} 
+0

आपकी समस्या क्या है? आपने क्या प्रयास किया है – shallowThought

+1

मुझे अपने कोड को संशोधित करने की आवश्यकता है जैसे कि यह आईओएस 9 में भी काम करता है। उपयोगकर्ता नोटिफिकेशन फ्रेमवर्क जो मैंने यहां उपयोग किया है केवल आईओएस 10 –

+0

में अलग-अलग दिनांक घटकों को सेट करने या स्थानीय नोटिस को कई बार कॉल करने के तरीके में कैसे मौजूद है? – ArgaPK

उत्तर

27

स्थानीय अधिसूचना में कोड है

आईओएस 11: - आप आईओएस 11 के लिए निम्नलिखित कोड का उपयोग कर सकते कोई परिवर्तन किसी भी तरह का धक्का और स्थानीय अधिसूचना में की आवश्यकता है

if #available(iOS 10.0, *) { 
    //iOS 10 or above version 
    let center = UNUserNotificationCenter.current() 
    let content = UNMutableNotificationContent() 
    content.title = "Late wake up call" 
    content.body = "The early bird catches the worm, but the second mouse gets the cheese." 
    content.categoryIdentifier = "alarm" 
    content.userInfo = ["customData": "fizzbuzz"] 
    content.sound = UNNotificationSound.default() 

    var dateComponents = DateComponents() 
    dateComponents.hour = 15 
    dateComponents.minute = 49 
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) 

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) 
       center.add(request) 
} else { 
    // ios 9 
    let notification = UILocalNotification() 
    notification.fireDate = NSDate(timeIntervalSinceNow: 5) as Date 
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!" 
    notification.alertAction = "be awesome!" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    UIApplication.shared.scheduleLocalNotification(notification) 

    let notification1 = UILocalNotification() 
    notification1.fireDate = NSDate(timeIntervalSinceNow: 15) as Date 
    notification1.alertBody = "Hey you! Yeah you! Swipe to unlock!" 
    notification1.alertAction = "be awesome!" 
    notification1.soundName = UILocalNotificationDefaultSoundName 

    UIApplication.shared.scheduleLocalNotification(notification1) 

} 

पुश अधिसूचना

import UserNotifications 

if #available(iOS 10.0, *) { 
    //iOS 10 or above version 
    UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { granted, error in 
      DispatchQueue.main.async { 
       if granted { 
        UIApplication.shared.registerForRemoteNotifications() 
       } 
       else { 
        //Do stuff if unsuccessful... 
       } 
      } 
    }) 
} 
else{ 
     //iOS 9 
     let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound] 
     let setting = UIUserNotificationSettings(types: type, categories: nil) 
     UIApplication.shared.registerUserNotificationSettings(setting) 
     UIApplication.shared.registerForRemoteNotifications() 

} 

UIApplicationDelegate

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    let tokenParts = deviceToken.map { data -> String in 
     return String(format: "%02.2hhx", data) 
    } 
    let token = tokenParts.joined() 
    print(token) 
} 

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 

} 

UNUserNotificationCenterDelegate

केवल iOS 10 में और संस्करण ऊपर उपलब्ध

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

} 
+0

अलग-अलग दिनांक घटकों को कैसे सेट करें या स्थानीय नोटिस को कई बार कैसे कॉल करें? – ArgaPK

9

मैंने इस कक्षा को स्विफ्ट 3 के लिए बनाया है जिसमें अधिसूचना को पुश करने और एक अधिसूचना अधिसूचना के लिए अनुमति का अनुरोध करने के लिए एक कार्य है। आईओएस 9 और आईओएस 10+ के लिए काम करता है।

import UIKit 
import UserNotifications 

class LocalNotification: NSObject, UNUserNotificationCenterDelegate { 

    class func registerForLocalNotification(on application:UIApplication) { 
     if (UIApplication.instancesRespond(to: #selector(UIApplication.registerUserNotificationSettings(_:)))) { 
      let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory() 
      notificationCategory.identifier = "NOTIFICATION_CATEGORY" 

      //registerting for the notification. 
      application.registerUserNotificationSettings(UIUserNotificationSettings(types:[.sound, .alert, .badge], categories: nil)) 
     } 
    } 

    class func dispatchlocalNotification(with title: String, body: String, userInfo: [AnyHashable: Any]? = nil, at date:Date) { 

     if #available(iOS 10.0, *) { 

      let center = UNUserNotificationCenter.current() 
      let content = UNMutableNotificationContent() 
      content.title = title 
      content.body = body 
      content.categoryIdentifier = "Fechou" 

      if let info = userInfo { 
       content.userInfo = info 
      } 

      content.sound = UNNotificationSound.default() 

      let comp = Calendar.current.dateComponents([.hour, .minute], from: date) 

      let trigger = UNCalendarNotificationTrigger(dateMatching: comp, repeats: true) 

      let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) 

      center.add(request) 

     } else { 

      let notification = UILocalNotification() 
      notification.fireDate = date 
      notification.alertTitle = title 
      notification.alertBody = body 

      if let info = userInfo { 
       notification.userInfo = info 
      } 

      notification.soundName = UILocalNotificationDefaultSoundName 
      UIApplication.shared.scheduleLocalNotification(notification) 

     } 

     print("WILL DISPATCH LOCAL NOTIFICATION AT ", date) 

    } 
} 

उपयोग:

आप कहीं भी अनुमति का अनुरोध कर सकते हैं:

LocalNotification.registerForLocalNotification(on: UIApplication.shared) 

और एक स्थानीय अधिसूचना प्रेषण करने के लिए:

LocalNotification.dispatchlocalNotification(with: "Notification Title for iOS10+", body: "This is the notification body, works on all versions", at: Date().addedBy(minutes: 2)) 

युक्ति:

आप किसी भी भविष्य में अधिसूचना को आग पर सेट कर सकते हैं, इस उदाहरण में मैं अधिसूचना आग में कुछ मिनटों में भविष्य की तारीख प्राप्त करने के लिए दिनांक विस्तार का उपयोग कर रहा हूं। यह है:

extension Date { 
    func addedBy(minutes:Int) -> Date { 
     return Calendar.current.date(byAdding: .minute, value: minutes, to: self)! 
    } 
} 
संबंधित मुद्दे