7

I need to show hyperlink in my remote notification along with Title and body. I have done some thing like this:

@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()
}

sample for hyper link "SHOW BILL"

In this I am sending url in user info but I need this url as hyperlink that shows when notification appears. and when I click on that hyperlink it will open this URL in webView. Loading URL in webview part is done. Just need to know how can I show this url as hyperlink on notification.

kindly help me out.

RJ168
  • 1,006
  • 2
  • 12
  • 22
hood
  • 65
  • 1
  • 9
  • 1
    From my side I'd recommend to use [Notification Content Extension](https://developer.apple.com/documentation/usernotificationsui/customizing_the_appearance_of_notifications) for customization of your notification. There are a lot of articles about it, but I don't know if it corrects for your flow. – Vadim Nikolaev Jan 21 '20 at 09:22
  • Yes I went through them... but the requirement doesn't allow to use extension... for some reason.... – hood Jan 22 '20 at 04:55
  • 1
    have you seen this [answer](https://stackoverflow.com/a/20277466/4056108) – chirag90 Feb 07 '20 at 14:15
  • @chirag: I have already done this chirag if you see the code. opening url is not a problem. Problem is to show hyperlink text in notification. – hood Feb 11 '20 at 08:29

1 Answers1

0

You cannot control the way a notification is shown by iOS, but you can declare specific actions for your notifications. See here: https://developer.apple.com/documentation/usernotifications/declaring_your_actionable_notification_types

This lets you add your custom 'Pay, Reject, Block' actions to the notification. iOS will offer the choices to the user, and notify your app in the background when the user selects one, but you will not be able to show the URL, only text.

The only way to show a custom dialog following a notification is if you get the notification while the app is in the foreground, because then the OS doesn't show the notification, it only notifies your app, and you can then decide to show whatever UI suits you. But that kind of goes against the idea of the notification, which can come in at any time, in particular when your app is not running.

Guy Moreillon
  • 993
  • 10
  • 28
  • I was thinking to use use rich notification to show a button in place of "SHOW BILL" and on click of that button open URL. I guess I can do this. But the problem is I am not able to call didreceive in my notification extension class. once didreceive gets caleed I can manage things but.... :( – hood Feb 11 '20 at 08:26
  • I think you're confusing the order of operations: the 'didReceive' callback gets called once the user has reacted to the notification, e.g. after she has chosen a custom action, not at the time of reception of the notification and therefor not before the notification is shown to the user but after. See here: https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter. – Guy Moreillon Feb 11 '20 at 08:54
  • A UNNotificationServiceExtension object provides the entry point for a Notification Service app extension, which lets you customize the content of a remote notification before it is delivered to the user. A Notification Service app extension doesn't present any UI of its own. Instead, it is launched on demand when a notification of the appropriate type is delivered to the user’s device. You use this extension to modify the notification’s content or download content related to the extension. https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension – hood Feb 11 '20 at 09:43
  • Ah, but the UNNotificationServiceExtension's didReceive callback is not the method you are implementing in your code above! You are implementing UNUserNotificationCenterDelegate didReceive method, they are not the same at all. – Guy Moreillon Feb 11 '20 at 13:08
  • Also, UNNotificationServiceExtension allows you to modify the notification before it is shown to the user, but it does not allow you to display a custom UI. As said in the documentation: "A Notification Service app extension doesn't present any UI of its own.", from https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension – Guy Moreillon Feb 11 '20 at 13:12
  • Yes above code I did because UNNotificationServiceExtension's did receive was not getting called. (Still it is not called), for a time being I did it to show the content i was getting in url but what i really want to do is to show a button or link in notification alert. I have implemented that code for UNNotificationServiceExtension too but its did receive is not being called and I am receiving that same old notification. – hood Feb 12 '20 at 05:26