【问题标题】:Swift - Local Notification doesn't get triggeredSwift - 本地通知不会被触发
【发布时间】:2017-05-13 09:39:05
【问题描述】:

我正在使用 Swift 3 进行编码,我只是想现在发送通知,没有任何延迟或间隔。但是,通知永远不会被触发。这是我的代码..

ViewController 代码

import UserNotifications

class HomeViewController: UIViewController{
    var isGrantedNotificationAccess:Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        UNUserNotificationCenter.current().requestAuthorization(
            options: [.alert,.sound,.badge],
            completionHandler: { (granted,error) in
                self.isGrantedNotificationAccess = granted
        })

        if isGrantedNotificationAccess{
            triggerNotification()
        }
    }

    //triggerNotification func goes here
}

触发通知功能:

func triggerNotification(){
    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey: "Notification Testing", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: "This is a test", arguments: nil)
    content.sound = UNNotificationSound.default()
    content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
    let trigger = UNTimeIntervalNotificationTrigger(
        timeInterval: 1.0,
        repeats: false)

    let request = UNNotificationRequest.init(identifier: "testTriggerNotif", content: content, trigger: trigger)

    let center = UNUserNotificationCenter.current()
    center.add(request)
}

我做错了什么?

【问题讨论】:

  • 你可以参考这个链接-stackoverflow.com/questions/37807302/…
  • 您的通知工作正常。对于测试,将时间间隔增加到 5 并在后台移动应用程序。(通知横幅将按预期显示。问题是您没有处理应用程序应该做什么然后用户通知到达并且应用程序处于前台状态)

标签: ios swift xcode notifications uilocalnotification


【解决方案1】:

您错过了处理,当应用处于前台时,您没有指定通知的外观或呈现方式。

在添加通知时设置以下行以指定您要在用户使用应用程序时显示横幅(iOS 10 新功能)。

在构造 UNMutableNotificationContent 对象时添加以下代码行:

content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")

您还应该在 AppDelegate 中添加以下方法:

// This method will be called when app received push notifications in foreground

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler(UNNotificationPresentationOptions.alert)        
}

【讨论】:

  • 我在哪里添加这个?在 viewDidLoad 里面?
  • 这个 setValue(forKey:) 方法是一个完全未记录的密钥,并且不保证将来(或根本)工作。也没有必要。 UNUserNotificationCenter 委托方法是您所需要的,并且已记录在案。您必须在您的应用代理中专门实现UNUserNotificationCenterDelegate,并将UNUserNotificationCenter 的代理设置为didFinishLaunchingWithOptions 中的应用代理才能完成此工作。
  • shouldAlwaysAlertWhileAppIsForeground 在 iOS 12 中不再支持并且代码崩溃。
  • @OrtwinGentz 你找到 IOS 12 的替代品了吗?
  • @theReverend 正如比尔提到的那样,实现willPresent 代表就足够了。
【解决方案2】:

UNUserNotificationCenter.current().requestAuthorization 的完成处理程序是异步工作的(方法在不同的线程中处理)。这就是为什么在完成处理程序完成之前读取代码中的“if isGrantedNotificationAccess..”语句的原因。

有一些方法可以解决这个问题。一种是使用 NotificationCenter 通知 HomeViewController 的 requestAuthorization 方法结束(见下例)。

import UserNotifications

class HomeViewController: UIViewController{

let notification = Notification.Name("requestAuthorizationComplete")

override func viewDidLoad() {
    super.viewDidLoad()

    // Register self as Observer to receive notification
    NotificationCenter.default.addObserver(self, selector: #selector(self.triggerNotification), name: notification, object: nil)

    UNUserNotificationCenter.current().requestAuthorization(
        options: [.alert,.sound,.badge],
        completionHandler: { (granted,error) in
        NotificationCenter.default.post(name: self.notification, object: nil)
    })

}

func triggerNotification(notification:NSNotification?){
 // this function will be called once requestAuthorization is complete
}

【讨论】:

  • 没有变化。仍然没有被触发
  • 我刚刚修复了一个错字通知 --> self.notification 并且它在我这边运行良好。请试试这个。
  • 奇怪,我的还没有被解雇。您能否也发布您的 triggerNotification 功能?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多