Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
How to differentiate between whether a notification is a local or remote notification when app Starts in IOS

Previously i used the following code to differentiation whether my notification is local or remote when the app starts

    func application(_ application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: 
    [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
    {


    }
    if (launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil)
    {


    }
    }

The conditions is that my app is killed and I am opening it from notification.

The problem is that this method

if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
{


}

is deprecated and the following method isnot called when the app is opened from notification center

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can check the notification type in userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: too,

Class hierarchy is:

UNNotificationResponse > UNNotification > UNNotificationRequest > UNNotificationTrigger

There are 4 types of triggers in UNNotificationRequest:

  • UNLocationNotificationTrigger
  • UNPushNotificationTrigger
  • UNTimeIntervalNotificationTrigger
  • UNCalendarNotificationTrigger

Just use,

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.notification.request.trigger is UNPushNotificationTrigger {
        print("remote notification");
    }

}
over 4 years ago · Santiago Trujillo Report

0

While creating localnotification set the identifier in the notification which can be used for identifying the difference in the handling notification

Following is example creating localnotification with identifier.

        let content = UNMutableNotificationContent()
        content.title = "Title"
        content.body = "Body"
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

        let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

Handling local notification with identifier.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.notification.request.identifier == "TestIdentifier" {
        print("handling notifications with the TestIdentifier Identifier")
    }

    completionHandler()

}

For Handling remote notification you can use the following line

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("handling notification")
    if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {
        let message = parseRemoteNotification(notification: notification)
        print(message as Any)
    }
    completionHandler()
}
 
private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {
    if let aps = notification["aps"] as? [String:AnyObject] {
        let alert = aps["alert"] as? String
        return alert
    }
 
    return nil
}

You can add an additional condition for handling both the notification in the same method by checking identifier in the first line.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("handling notification")
 if response.notification.request.identifier == "TestIdentifier" {
        print("handling notifications with the TestIdentifier Identifier")
    }else {
    if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {
        let message = parseRemoteNotification(notification: notification)
        print(message as Any)
    }
}
    completionHandler()
}
 
private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {
    if let aps = notification["aps"] as? [String:AnyObject] {
        let alert = aps["alert"] as? String
        return alert
    }
 
    return nil
}
over 4 years ago · Santiago Trujillo Report

0

You can set your local notifications key values in content.userInfo.

content.userInfo = ["isMyLocationNotification" : true] //you can set anything

Then check in didReceive response method of UNUserNotificationCenterDelegate:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            print(response.notification.request.content.userInfo) //you can check your notification types
        }

In output section you will user Information data with isMyLocationNotification key, Now you can identify weather notification is local of remote.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!