1

In our project, we want to changed a title and body of Remote Notification . In that we generate a Local Notification and Display a local notification with changed a title and body and hide push Notification. But in that while App is in Background and kill State it will display a Remote a Notification instead of Local Notification. But we want to display a Local Notification instead of push in Will present Notification. how to do this ?

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

        if notification.request.identifier != "local_notification1"{
                self.isAdded = false
        }
            let name = (ContactsManager.shared.getContacName(withPhoneNumber:notification.request.content.title) != nil) ? ContactsManager.shared.getContacName(withPhoneNumber:notification.request.content.title) :
                notification.request.content.title
            let notificationContent = UNMutableNotificationContent()
            //        notificationContent.userInfo = notification.request.content.userInfo
            notificationContent.body = notification.request.content.body
            notificationContent.title = name!
            debugPrint("name title is %@ ", name)
            debugPrint("notificationContent title is %@ ", notificationContent.title)
            notificationContent.sound = .default

            let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
            let notificationRequest = UNNotificationRequest(identifier: "local_notification1", content: notificationContent, trigger: notificationTrigger)

            if !isAdded {
                UNUserNotificationCenter.current().add(notificationRequest) { (error) in
                    if let error = error {
                        debugPrint("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
                    }else {
                        print("is Shown")
                    }
                    self.isAdded = true
                }
                 completionHandler([])

            }
         completionHandler([.alert,.sound])
   }    
}
2
  • I don't think that a Push Notification can silently trigger a Local Notification. The user can click on the Push Notification and then, you can add the code that can trigger the Local Notification.
    – Rob
    Commented Mar 6, 2020 at 10:49
  • I mean to say to in willPresent method when Push occur. we want to display a Local Notification instead of Push Banner. Commented Mar 6, 2020 at 10:52

1 Answer 1

1

You can modify content of remote notification with help of UNNotificationServiceExtension

  1. First override didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

  2. Modify content and.

  3. Return updated content in contentHandler

Note: Required iOS 10+

6
  • 1
    I don't want to use Service Extension. I want to Display a local Notification when Push notification receive. Because I have Functionality related to user Default which is not accessible in Service extension from my App that's why. Commented Mar 11, 2020 at 8:33
  • Not possible without Service Extension
    – SPatel
    Commented Mar 11, 2020 at 9:48
  • and you can access userdefault in Service Extension
    – SPatel
    Commented Mar 11, 2020 at 9:48
  • read this one->stackoverflow.com/questions/45607903/…
    – SPatel
    Commented Mar 11, 2020 at 9:49
  • You can use silent notification! but i don't think it's reliable because if app is killed notification may not deliver at all
    – SPatel
    Commented Mar 11, 2020 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.