13.9 C
New York
Saturday, October 19, 2024

swift – Push Notification Not Displayed in Background (iOS) Regardless of Correct Setup


I’m having hassle displaying push notifications when my iOS app is within the background or in sleep mode. Notifications work effective within the foreground, however they don’t present up within the background in any respect. I’ve verified my setup a number of occasions, together with notification permissions, payload format, and background modes, however the concern persists.

What I Have Tried:
APNs Payload: The backend sends the next payload to APNs:

{
  "mediaDomain": "your_domain",
  "sendTime": "1633024800",
  "chatData": "chat_data_here",
  "time": "1633024800",
  "uuid": "unique_id",
  "senderPublicKey": "public_key",
  "receiverPublicKeyIdentifier": "receiver_id",
  "senderPublicKeyIdentifier": "sender_id",
  "model": "1",
  "chatID": "12345",
  "senderID": "67890",
  "receiverID": "54321",
  "isRetain": "false",
  "sort": "chat_message",
  "aps": {
    "alert": {
      "title": "palphone",
      "physique": "You could have one new message"
    },
    "sound": "default",
    "content-available": 1
  }
}

Notification Kind: chat_message.
I count on this payload to show a notification with an alert and sound, but it surely would not work when the app is within the background.
AppDelegate Setup:
I’ve carried out the mandatory strategies in AppDelegate for dealing with push notifications:

func utility(_ utility: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if let sort = userInfo["type"] as? String, sort == "chat_message" {
        // Course of notification
        let content material = UNMutableNotificationContent()
        content material.title = "New Message"
        content material.physique = "Verify your messages."
        content material.sound = .default
        
        let request = UNNotificationRequest(identifier: UUID().uuidString, content material: content material, set off: nil)
        UNUserNotificationCenter.present().add(request, withCompletionHandler: nil)
    }
    completionHandler(.newData)
}

Background Modes:
Distant notifications background mode is enabled in Signing & Capabilities.
Notification Permissions:
I’m requesting notification permissions accurately in didFinishLaunchingWithOptions:

UNUserNotificationCenter.present().requestAuthorization(choices: [.alert, .badge, .sound]) { granted, error in
    if granted {
        DispatchQueue.major.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    } else {
        print("Notification permission denied.")
    }
}

Testing on Actual System:
I’m testing on an actual gadget, and notifications aren’t working within the background, however they work when the app is within the foreground.

What I Anticipated:
I count on to obtain a visual notification with an alert and sound when the app is within the background, identical to when it is within the foreground.

What Is Occurring As an alternative:
No notification is displayed when the app is within the background or asleep. The didReceiveRemoteNotification technique isn’t being known as both.

My Atmosphere:
Xcode model: 16.0.
iOS model: 18.0
System: iPhone 16
Push Notification Service: Firebase / Customized APNs implementation

Further Notes:
I’ve checked the logs from APNs, and the push notification is being delivered efficiently to the gadget. It simply isn’t being displayed.
content-available: 1 is included within the payload for background processing, however no native notification is being triggered.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles