锁屏ios上的本地通知

Posted

技术标签:

【中文标题】锁屏ios上的本地通知【英文标题】:Local Notification on locked screen ios 【发布时间】:2018-04-11 10:46:40 【问题描述】:

我想在 ios 中屏幕锁定时发送本地通知。 以下是我添加的代码。但是屏幕锁定时无法收到通知

let notification = UILocalNotification()
notification.alertAction = "Go back to App"
notification.alertBody = "Phone Found..!!"

notification.fireDate = NSDate(timeIntervalSinceNow: 1) as Date
UIApplication.shared.scheduleLocalNotification(notification)
notification.soundName = UILocalNotificationDefaultSoundName 

请建议,我错过了什么。 提前谢谢你。

【问题讨论】:

timeIntervalSinceNow: 1,您如何能够运行此代码并在一秒钟内锁定您的设备?尝试增加它,然后再试一次...可能在应用程序打开时被触发 【参考方案1】:

您的代码有效,您必须增加时间间隔,UILocalNotification 不推荐使用UNUserNotificationCenter

UNUserNotificationCenter.current().requestAuthorization(options: [.alert])
     (success, error) in
        if success 
            print("Permission Granted")
         else 
            print("There was a problem!")
        
    


    let notification = UNMutableNotificationContent()
    notification.title = "title"
    notification.subtitle = "subtitle"
    notification.body = "the body."

    let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

【讨论】:

【参考方案2】:

您可以使用共享的 UNUserNotificationCenter 对象来安排本地通知。 您需要将 Usernotifications 框架导入到您的 swift 文件中,然后您可以请求本地通知。

import UserNotifications

您需要在 AppDelegate 的 didFinishLaunchingWithOptions 中调用该函数

registerForLocalNotifications()

registerForLocalNotifications() 的定义

  func registerForLocalNotifications() 
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) 
        (granted, error) in
        print("Permission granted: \(granted)")

        guard granted else return

        self.getNotificationSettings()
    


func getNotificationSettings() 
    UNUserNotificationCenter.current().getNotificationSettings  (settings) in
        print("Notification settings: \(settings)")
        guard settings.authorizationStatus == .authorized else return

        // UIApplication.shared.registerForRemoteNotifications()
    

然后您可以像这样创建通知内容和请求通知。

let content = UNMutableNotificationContent()
content.title = "Title of Notification"
content.body = "Body of Notification"
content.sound = UNNotificationSound.default()


let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true)           


let request = UNNotificationRequest(identifier: "Identifier of notification", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: (error) in
                    if let error = error 
                        print("SOMETHING WENT WRONG")
                    
                )

【讨论】:

以上是关于锁屏ios上的本地通知的主要内容,如果未能解决你的问题,请参考以下文章

处理锁屏ios上的推送通知

iOS 上的每日本地通知

iOS 14:如何延迟“[App Name] 想要查找并连接到本地网络上的设备”通知?

本地推送通知UILocalNotification

无法查看 iOS 10 本地通知的操作按钮

一次创建多个本地通知失败