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])
   }    
}
SPatel
  • 4,768
  • 4
  • 32
  • 51
  • 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 Mar 06 '20 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. – Khushbu Patel Mar 06 '20 at 10:52

1 Answers1

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+

SPatel
  • 4,768
  • 4
  • 32
  • 51
  • 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. – Khushbu Patel Mar 11 '20 at 08:33
  • Not possible without `Service Extension` – SPatel Mar 11 '20 at 09:48
  • and you can access userdefault in Service Extension – SPatel Mar 11 '20 at 09:48
  • read this one->https://stackoverflow.com/questions/45607903/sharing-userdefaults-between-extensions – SPatel Mar 11 '20 at 09: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 Mar 11 '20 at 09:53
  • https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html – SPatel Mar 11 '20 at 09:58