首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >兼容IOS版本的本地和推送通知

兼容IOS版本的本地和推送通知
EN

Stack Overflow用户
提问于 2017-03-09 15:02:52
回答 2查看 32K关注 0票数 20

我在iOS 10中开发了local Notifications。它工作得很完美。但现在我应该如何编码local notificationspush notification,如果用户使用的是iOS 9和更高版本。有谁能帮帮忙吗?

以下是iOS 10中的代码

代码语言:javascript
运行
复制
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)
        }

     }

 }
EN

Stack Overflow用户

发布于 2017-07-29 03:50:04

我为Swift 3做了这个类,它有一个请求允许推送通知和发送通知的功能。适用于iOS 9和iOS 10+。

代码语言:javascript
运行
复制
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)

    }
}

使用:

您可以在以下位置请求权限:

代码语言:javascript
运行
复制
LocalNotification.registerForLocalNotification(on: UIApplication.shared)

并发送本地通知:

代码语言:javascript
运行
复制
LocalNotification.dispatchlocalNotification(with: "Notification Title for iOS10+", body: "This is the notification body, works on all versions", at: Date().addedBy(minutes: 2))

提示:

您可以将通知设置为在任何未来日期触发,在本例中,我使用日期扩展来获取通知触发的未来日期(分钟)。就是这样:

代码语言:javascript
运行
复制
extension Date {
    func addedBy(minutes:Int) -> Date {
        return Calendar.current.date(byAdding: .minute, value: minutes, to: self)!
    }
}
票数 13
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42688760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档