【问题标题】:Show Hyper Link in remote notification with Message在带有消息的远程通知中显示超链接
【发布时间】:2020-05-06 14:11:50
【问题描述】:

我需要在远程通知中显示超链接以及标题和正文。我做过这样的事情:

@IBAction func openPDFButtonPressed(_ sender: Any) {
    self.scheduleNotification()         
}

func scheduleNotification() {
    let center = UNUserNotificationCenter.current()

    let content = UNMutableNotificationContent()
    content.title = "Download The Receipt"
    content.body = "Kindly Download your purchase bill"
    content.categoryIdentifier = "PDF"
    content.userInfo = ["customData": "http://www.pdf995.com/samples/pdf.pdf"]
    content.sound = UNNotificationSound.default

    var dateComponents = DateComponents()
    dateComponents.hour = 10
    dateComponents.minute = 30
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

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

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // pull out the buried userInfo dictionary
    let userInfo = response.notification.request.content.userInfo
    print("Test: \(userInfo)")

    if let customData = userInfo["customData"] as? String {
        print("Custom data received: \(customData)")


        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "PDFViewController") as! PDFViewController
        newViewController.strURL = customData
        self.present(newViewController, animated: true, completion: nil)
    }
    completionHandler()
}

在此我在用户信息中发送 url,但我需要此 url 作为超链接,当通知出现时显示。当我单击该超链接时,它将在 webView 中打开此 URL。 在 webview 部分中加载 URL 已完成。只需要知道如何将此网址显示为通知上的超链接。

请帮帮我。

【问题讨论】:

  • 就我而言,我建议使用Notification Content Extension 来自定义您的通知。有很多关于它的文章,但我不知道它是否适合您的流程。
  • 是的,我通过了它们...但是要求不允许使用扩展名...由于某种原因....
  • 你看过这个answer
  • @chirag:如果你看到代码,我已经完成了这个 chirag。打开网址不是问题。问题是在通知中显示超链接文本。

标签: ios objective-c swift xcode rich-notifications


【解决方案1】:

您无法控制 iOS 显示通知的方式,但您可以为通知声明特定操作。见这里:https://developer.apple.com/documentation/usernotifications/declaring_your_actionable_notification_types

这使您可以将自定义的“支付、拒绝、阻止”操作添加到通知中。 iOS 会向用户提供选择,并在用户选择一项时在后台通知您的应用,但您将无法显示 URL,只能显示文本。

在通知之后显示自定义对话框的唯一方法是,如果您在应用程序处于前台时收到通知,因为这样操作系统不会显示通知,它只会通知您的应用程序,然后您可以决定显示任何适合您的 UI。但这种做法违背了通知的理念,通知可以随时出现,尤其是在您的应用未运行时。

【讨论】:

  • 我正在考虑使用丰富的通知来显示一个按钮来代替“SHOW BILL”,然后单击该按钮打开 URL。我想我可以做到这一点。但问题是我无法在通知扩展类中调用 didreceive。一旦 didreceive 得到通知,我可以管理事情,但是.... :(
  • 我认为您混淆了操作顺序:一旦用户对通知做出反应,就会调用“didReceive”回调,例如在她选择了自定义操作之后,不是在收到通知时,因此不是在通知显示给用户之前,而是之后。见这里:developer.apple.com/documentation/usernotifications/….
  • UNNotificationServiceExtension 对象提供了通知服务应用程序扩展的入口点,允许您在将远程通知传递给用户之前对其内容进行自定义。 Notification Service 应用程序扩展不提供任何自己的 UI。相反,当适当类型的通知被传递到用户的设备时,它会按需启动。您使用此扩展程序来修改通知的内容或下载与扩展程序相关的内容。 developer.apple.com/documentation/usernotifications/…
  • 啊,但是 UNNotificationServiceExtension 的 didReceive 回调不是您在上面的代码中实现的方法!您正在实现 UNUserNotificationCenterDelegate didReceive 方法,它们根本不一样。
  • 此外,UNNotificationServiceExtension 允许您在通知显示给用户之前对其进行修改,但它不允许您显示自定义 UI。正如文档中所说:“通知服务应用程序扩展不提供任何自己的 UI。”,来自developer.apple.com/documentation/usernotifications/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 2011-05-15
  • 2017-11-06
  • 2021-07-14
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多