Notification Service Extension Not working
Asked Answered
P

6

8

Notification is not being displayed when I send mutable-content:1 with push payload neither it hits the breakpoint inside the Notification service extension, although without mutable-content push is being displayed, also Notification content extension is working fine. I did not modify the code inside the Notification service extension it's the default generated by the Xcode. Am I missing something while creating a notification service extension or it might be the issue with the device setting? I have tested on the same device a few days ago and the notification service extension was working fine.

Below is my code for service extension

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
    
           contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

edit 1: below is my push payload

{  
   "aps":{  
      "sound":"default",
      "mutable-content": 1,
      "category": "myCategory",
      "alert":{  
         "body":"this is a custom push",
         "subtitle":"subtitle of the push",
         "title":"Push Test"
      }
      
   }
}

edit1: The problem seems on that particular device running on 10.2.1, I checked with other device running on 10.2, it was triggering the Notification service extension.

Patinated answered 9/3, 2017 at 12:35 Comment(5)
How are you sending the Json containing "mutable-content:1" ? If it's with FCM change "-" by "_"Cantonese
Hi @Cantonese i am not using FCMPatinated
Hi @princerk I am also trying to implement Notification Service Extension but it's not modifying my content even I am using the default apple code. Do I need to enable Push Notification for the App Id I set for notification service extension? Please help me with the steps required to make it work.Persas
@Milan No need to enable the push notification for the service extension. Default code generated by xcode should do the work. make sure you are passing "mutable-content": 1 in your push payload. Try with other devices.Patinated
FWIW, you're not showing how you're modifying the payload.Vachil
P
26

Finally I was able to resolve the issue. I am not sure what was causing the issue but after some experiments I realised that not only my service extension but all other installed app's service extension was not working and when sending the push payload with "mutable-content: 1, Notification was not displayed. After rebooting the device it started working fine. I was using iPhone 6s.

Patinated answered 16/3, 2017 at 12:49 Comment(1)
Recently some of my customer has same issue like this, and everything is working fine after rebooting. Do you find the root reason?Pahoehoe
V
25

Deployment target of your NotificationServiceExtension can also be a reason.

In my case, I was testing on Device running on iOS 12.4 while target version of my NotificationServiceExtension was 14.1. I Just changed my NotificationServiceExtension targeted version to 12.4 it Worked.

Vaulting answered 13/5, 2021 at 14:37 Comment(2)
OMG That's exactly what caused the issue for me. 😱Brahma
WEEKS trying to figure out why it wasn't working on our QA flows, and this was the answer for me. Thanks!Evaginate
P
3

In my case I removed extension and add again. Then restart device..

enter image description here

Psychoneurosis answered 27/5, 2022 at 7:10 Comment(1)
This is the only solution that worked for me 6/22/2022. After my app transfer, I had to remove this file and re-add it. Then the notification extension started working again.Ciel
S
1

In my case, when I was debugging, the notification service extension was not getting launched by the system when it received a notification with the mutable content payload. I tried a lot and then figured out that in the run script, the Copy only when installing was checked. I unchecked it and I was able to debug the extensions.

enter image description here

Satanism answered 10/2, 2023 at 13:7 Comment(0)
A
0

In my case it ended up being my extension using too much memory. Console.app was only showing that Springboard was killing the app with an error like:

CMSessionMgr- CMSessionMgrHandleApplicationStateChange: CMSession: Sending stop command to _ with pid '_' because client is not allowed to play in the background AND does not continue AirPlaying video when device locks

I thought something was wrong with the entitlements or something like that, but once I debugged it I saw that it was iOS killing the process for taking too much memory. Once I fixed that bug, the extension was kept alive until I called the callback.

Anxiety answered 16/4, 2022 at 12:54 Comment(0)
I
0

May not relate to the general issue if it exists, but I've spent a whole day trying to figure out why AppMetrica Push SDK campaign pushes won't come in TestFlight but come successfully when running from Xcode. For me it was "push environment" not being set to "production", but for "development":

in func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) had to change it to:

let pushEnvironment = YMPYandexMetricaPushEnvironment.production
YMPYandexMetricaPush.setDeviceTokenFrom(deviceToken, pushEnvironment: pushEnvironment)
Indubitability answered 3/8, 2023 at 8:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.