Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

316
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda