【问题标题】:Local Notification on locked screen ios锁屏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,您如何能够运行此代码并在一秒钟内锁定您的设备?尝试增加它,然后再试一次...可能在应用程序打开时被触发

标签: ios swift notifications localnotification


【解决方案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")
                        }
                    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多