biggreentree
biggreentree

Reputation: 1943

why swift willPresent notification method shows two different local notifications when in background?

I'm testing on real device, via apple notification console, my issue is:

app closed - I see the "app" notification, if I tap on it, opp opens and shows another notification with the "special price" data.

app in foreground - both notifications, in same order.

since adding willAppear if app is in foreground, I see two local notifications the first one is a plain notification with standard "aps" data, the second on is custom with my "special" and "price" data.

MY PAYLOAD

{
    "aps": {
        "alert": {
            "title": "Our new special!",
            "subtitle": "special sale",
            "body": "body"
        },
        "badge": 1,
        "sound": "default"
    },
      "special": "avocado_bacon_burger",
      "price": "9.99"
}

methods

//triggered when a notification is opened
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    Logger.info("didReceive")
    let userInfo = response.notification.request.content.userInfo
    
    guard let specialName = userInfo["special"] as? String,
          let specialPriceString = userInfo["price"] as? String,
          let specialPrice = Float(specialPriceString) else {
        //always call the completionHandler, anyway!
        completionHandler()
        return
    }
    
    let item = Item(specialName: specialName, specialPrice: specialPrice)
    //addItemToCart
    //show the cart VC
    let itemDict: [String: Any] = ["special": item.specialName, "price": String(item.specialPrice)]

    self.handleNotification(userInfo: itemDict)
    completionHandler() //always call the completionHandler, anyway!
}

//triggered if app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    
    // managing extra data
    if let specialName = userInfo["special"] as? String,
       let specialPriceString = userInfo["price"] as? String,
       let specialPrice = Float(specialPriceString) {
        //update ui or do stuff
        print("Special: \(specialName), Price: \(specialPrice)")
        
        let item = Item(specialName: specialName, specialPrice: specialPrice)
        let itemDict: [String: Any] = ["special": item.specialName, "price": String(item.specialPrice)]
        self.handleNotification(userInfo: itemDict)
        
    }

    // how to present notification
    completionHandler([.alert, .sound, .badge])
}

func handleNotification(userInfo: [String: Any]) {
    Logger.info("called handleNotification")
    if let specialName = userInfo["special"] as? String,
       let specialPriceString = userInfo["price"] as? String,
       let specialPrice = Float(specialPriceString) {
        
        let content = UNMutableNotificationContent()
        content.title = "Special Offer"
        content.body = "Name: \(specialName), Price: \(specialPrice)"
        content.sound = .default
        
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
        UNUserNotificationCenter.current().add(request) { error in
            if let error = error {
                print("Error adding local notification: \(error.localizedDescription)")
            }
        }
    }
}

Upvotes: 0

Views: 34

Answers (0)

Related Questions