iOS10 UNNotificationServiceExtension not called
N

20

30

Im implementing new iOS10 extension to use rich notifications. Im trying to test it on push notifications but is not working, I just receive a simple notification and is not going through the extension.

I did all that it's specified in the official sites and some other places:

  • I have my app up and running with push notifications and the right provisioning profile
  • I added a new target to my app, a Notification Service Extension
  • Implemented my own code (it doesn't matter really because is not even entering to the new class)
  • Also I had to set a provisioning profile for this extension, I just use one with a wildcard, I don't see any documentation specifying if the extension target has to enable push notifications capability, in that case I would need a specific provisioning for this one, at the moment I just use a wildcard prov, anyway it matches (it must match) the profile I use in the app target, and push notifications capability is enabled for the app target only.
  • I added UNNotificationExtensionCategory and NSExtensionPointIdentifier. Also Im sending the category as part of the push payload from the server.

As I said, I get the notification but never goes through the extension. I see how the OS tries to load the extension but then throws an error with no relevant description to identify the problem:

Dec 31 21:00:00 iPhone SpringBoard(libextension.dylib)[51] <Notice>: calling plugIn beginUsing:   
Dec 31 21:00:57 iPhone pkd[86] <Notice>: assigning plug-in com.test.app.NotificationWithAttachmentExtension(1.0) to plugin sandbox   
Dec 31 21:03:57 iPhone pkd[86] <Notice>: enabling pid=51 for plug-in com.test.app.NotificationWithAttachmentExtension(1.0) 38BB5FF1-2597-42E0-B950-169DBFA80573 /private/var/containers/Bundle/Application/A8C47706-C0EC-4FB1-ABA7-0118372F6900/testapp.app/PlugIns/NotificationWithAttachmentExtension.appex   
Dec 31 21:00:53 iPhone SpringBoard(PlugInKit)[51] <Notice>: plugin com.test.app.NotificationWithAttachmentExtension interrupted   
Dec 31 21:03:56 iPhone SpringBoard(PlugInKit)[51] <Notice>: Hub connection error Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.test.app.NotificationWithAttachmentExtension" UserInfo={NSDebugDescription=connection to service named com.test.app.NotificationWithAttachmentExtension}   
Jun 29 13:33:36 iPhone SpringBoard(libextension.dylib)[51] <Notice>: PlugInKit error in beginUsing:   
Jun 17 23:33:04 iPhone SpringBoard(libextension.dylib)[51] <Notice>: killing invalid plugIn   
Dec 31 21:00:00 iPhone SpringBoard(UserNotificationsServer)[51] <Error>: Extension error whilst trying to modify push notification F502-9B36: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.test.app.NotificationWithAttachmentExtension" UserInfo={NSDebugDescription=connection to service named com.test.app.NotificationWithAttachmentExtension}   
Dec 31 21:00:00 iPhone SpringBoard(UserNotificationsServer)[51] <Notice>: [com.test.app] Saving notification F502-9B36   
Dec 31 21:00:00 iPhone SpringBoard(libextension.dylib)[51] <Notice>: completed calling plugIn beginUsing: for pid: 0  

Relevant extension .plist:

  <dict>
    <key>NSExtensionAttributes</key>
    <dict>
      <key>UNNotificationExtensionCategory</key>
      <string>attachmentCategory</string>
      <key>UNNotificationExtensionInitialContentSizeRatio</key>
      <real>1</real>
    </dict>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.usernotifications.service</string>
        <key>NSExtensionPrincipalClass</key>
        <string>$(PRODUCT_MODULE_NAME).NotificationService</string>
  </dict>

What's wrong or missing?

Nephridium answered 23/9, 2016 at 15:0 Comment(7)
Try adding this: <key>UNNotificationExtensionDefaultContentHidden</key> <true/> to NSExtensionAttributesSolander
Did you ever find a solution? If so could you please describe it or share a link as we are running into a similar issue...Silicious
@BenMarten, finally I have this working correctly, and this is what I remember from this issue:Nephridium
comment is below as an answer, it was too long to put it as a comment...Nephridium
A bit offtopic, but how do you get the springboard log? Dec 31 21:00:00 iPhone SpringBoardLefler
@JuanPedroLozano It's from the Device's logs - You can find it at Window -> Devices -> Select current device.Baroque
Apparently the extension runs as a separate app (Which is why breakpoints aren't hit). But I did learn that sending a category with your notification makes it so the app doesn't display action buttons or images.Cupriferous
N
16

Finally I have this working correctly, and this is what I remember from this issue.

1) Do not use devices with iOS10 beta version, because one of the problems I had was because I was using a beta version.

2) only the app requres APNS entitlements, this is not required for the privisoning used for the extension.

3) I was using a provisioning profile matching the id of the extension (not wildcard), anyway I cannot confirm if it works fine or not with wildcard.

4) NSExtensionAttributes are not required, just use NSExtensionPointIdentifier and NSExtensionPrincipalClass for the extension .plist. Unless you are using your own layout

5) This is working even using iOS 9 token registration methods.

6) don't forget mutable-content value in the payload coming in the push notification, this is the only mandatory value you need from the server to go through the extension.

I think this covers all the problems I had

Nephridium answered 20/12, 2016 at 18:5 Comment(4)
we need to create separate bundle id and provisioning profile in member center for UNNotificationServiceExtension ?Mizzenmast
APNs is required for app extension, or else it will NOT compile.Fugleman
same not working please can someone share the code of app delegate what he did????????Florous
set everything correctly. But still its not working :(Aesop
B
76

What also might do the trick is check your deployment target for the extension. Mine was set at 10.2 while the device I was testing on was (still) using 10.1

After altering the deployment target to 10.0 the UNNotificationServiceExtension instance was called perfectly

Broach answered 2/2, 2017 at 14:28 Comment(10)
Great. I did a same mistake. Thanks you so much.Avenue
Oh my God! This is really nice and simple solution. I didn't even think to check this.Complicacy
Thank you! THANK YOU SO MUCH @Broach I integrated a push library for one of the targets and then had to reintegrate for the second target during the same week. BOTH TIMES I wasted so much time with not checking the target and ended up on your answerMerrythought
Thank you so much it helped me a lot. I was using 10.1 device when extension deployment target was 10.3Doityourself
Face palm. I'm in this issue the 3rd time. This answer helped me the 2nd time. Next time I promise to send cake. 🎂Montana
After wasting a couple of days, this finally fixed it. Thanks you @BroachAutotype
I think this happens, because Xcode is setting it to the latest SDK when you create the extension. Therefore i ran into the same issue on one of my testing devices ...Pouched
you good but if you attach the screen shot its will aslo very helpfullFlorous
Thank you, I'm working in the tutorial I follow the exact the same but not working. You same my days. @BroachUnifilar
@Broach Thank you that worked in still 2022 😂Ie
M
28

And if you've done everything correctly, don't forget to attach it to the process.

After running the app that contains the extension:

  1. Set your breakpoint in the extension
  2. Select Debug / Attach to Process by PID or name
  3. Enter the name of the extension target
  4. Trigger the push notification
Miscellany answered 9/5, 2017 at 20:38 Comment(5)
+1 This answer was super helpful as I forgot that this is a separate process and was expecting breakpoints to work if I was just running the app process.Ever
NotificationService class is not appearing there!!Pastern
try adding it manually, when you start typing the name it should appear @Basir AlamFlightless
@BasirAlam from here If you're trying to debug an extension that is trigged by the OS then you need to do something so that the OS triggers it. Only then you can attach it to the debugger.Schiffman
was not aware that you could do this, thanks buddy!Excel
N
16

Finally I have this working correctly, and this is what I remember from this issue.

1) Do not use devices with iOS10 beta version, because one of the problems I had was because I was using a beta version.

2) only the app requres APNS entitlements, this is not required for the privisoning used for the extension.

3) I was using a provisioning profile matching the id of the extension (not wildcard), anyway I cannot confirm if it works fine or not with wildcard.

4) NSExtensionAttributes are not required, just use NSExtensionPointIdentifier and NSExtensionPrincipalClass for the extension .plist. Unless you are using your own layout

5) This is working even using iOS 9 token registration methods.

6) don't forget mutable-content value in the payload coming in the push notification, this is the only mandatory value you need from the server to go through the extension.

I think this covers all the problems I had

Nephridium answered 20/12, 2016 at 18:5 Comment(4)
we need to create separate bundle id and provisioning profile in member center for UNNotificationServiceExtension ?Mizzenmast
APNs is required for app extension, or else it will NOT compile.Fugleman
same not working please can someone share the code of app delegate what he did????????Florous
set everything correctly. But still its not working :(Aesop
K
11

If you are using Firebase, then try changing the payload as:

{
   “aps” : {
      “category” : “SECRET”,
      “mutable_content” : true,
      “alert” : {
         “title” : “Secret Message!”,
         “body”  : “(Encrypted)”
     },
   },
   “ENCRYPTED_DATA” : “Salted__·öîQÊ$UDì_¶Ù∞è   Ω^¬%gq∞NÿÒQùw”
}

The mutable_content field maps to the mutable-content field on APNs. For more details, go through this link.

Karakorum answered 14/11, 2018 at 13:32 Comment(3)
Using the regular non-firebase API, the field is named "mutable-content" and must be adjacent to the "alert" attributeMommy
@CalS right! Apparently, if the value of "mutable-content" is anything different from "false" or 0, the notification is considered to have a mutable content.Subjectivism
I think this is only for the legacy HTTPS API -- the v1 API requires you to pass Apple's own values through the aps dictionary, i.e. "mutable-content": 1Epochmaking
F
10

Swift 5+ Easy way

(in 2019 i have tried on ios10 and in 2024 i am working on ios 17+)

FOR ATTACHMENT OF PID FOLLOW THE STEP IN PICTURE VIA SELECTING YOU XCODE

 TODO: - First way to attach the Notification service if you now the pid or process name

 After running the app that contains the extension
 Set your breakpoint in the extension
 Select Debug / Attach to Process by PID or name
 Enter the name of the extension target
 Trigger the push notification

enter image description here

if Not worked upper mentioned Trick 
2nd Way to use debugger in notification extension

1) Run service extension as the Target instead of the app. Then it will ask for which app you have run service extension, then select your app and it will send the notification.

2) Make sure the deployment target of the service extension is less than your physical device's OS version.

3) Ensure payload contains 'mutable-content: 1'

4) Use FireBase Console for sending Push Notification (Which is authentic) No Need for 3rd Party Notification Service

//TODO: - in my case i was using 3rd party and waste my 2 days

//TODO: - 3rd Way

. this is very easy way just do it like this Firebase Side should be like:

 {
      "apns":{
          "payload":{
              "aps":{
                  "subtitle":"Session 707",
                  "mutable-content": 1, //Important Line
                  "sound":"true"
               }
       },
       "fcm_options":{
           "image":"https://i.sstatic.net/YxaLI.png"
       }
    }
 }

enter image description here

Florous answered 23/9, 2016 at 15:0 Comment(1)
This work for me, make me like a fool :joy:Antidepressant
B
10

You must set the deployment target to be the same on all your targets. I wonder why this is not done automatically by XCode :/ .. Apple loves to waste the developers' time smh...

Botulin answered 26/8, 2020 at 10:13 Comment(3)
saved my day. I had 14.0 on my main app and 14.1 on the service extension. Was not working. Changed service extension to 14.0 and it worked immediately. Thanks!Stypsis
OMG HOW THIS IS A THING THANK YOUHelios
THANKS! this worked! Incredible that Xcode doesn't do this automatically :(Buddle
M
9

Came here the second time. The first time, this answer helped me, the second time, it didn't. After a lot of (internal) swearing I found out that I had somehow accidentally removed the extension from Embedded Binaries in my main app target. When I added the extension back, my extension would be called again.

So check this:

  1. Click on your app project to the left.
  2. Click on your main app target.
  3. Choose General.
  4. Under Embedded Binaries, make sure your extension is listed, if not, add it.
Montana answered 15/12, 2017 at 7:38 Comment(0)
S
7

It seems like your plist is mixing 2 plists. There are 2 extensions in play:

  1. Notification Content Extension - responsible for displaying the content via a view controller
  2. Notification Service Extension - responsible for fetching content in the background before notification is displayed

Here is the plist for Notification Content Extension target:

enter image description here

Here is the plist for Notification Service Extension target:

r

Solander answered 7/10, 2016 at 15:57 Comment(5)
but in my case Im not using a custom UI, Im just using the extension to modify the content only (actually is not even going through my code, so let's say Im just sending the content as it is). now I see NSExtensionAttributes is requires for content extension, but looks like this is not my case. This is the first documentation I followed and when the post started, where my implementation didn't work: developer.apple.com/reference/usernotifications/…Nephridium
link is broken. are you sending mutable-content: 1 in the aps payload?Solander
yes, in fact the error I see in the device log if when the OS already found my extension but didn't run the code. So mutable-content was validated already at that time (without this will not even check for notif extensions)Nephridium
have you figured it out ?Fading
Did u found any solution for this?Kimkimball
T
7

Had the same problem, what solved it for me was removing the extension from Embedded Binaries and adding it again.

Thaw answered 12/6, 2019 at 15:52 Comment(4)
This. Unbelievable, but THIS is what solved this for me. I was banging my head against the wall for two days.Odel
After looooong long hours this was the fix for me. I've implemented rich push a lot of times. But this time somehow it didn't work for my new project. This answer changed something now it is working. Thank you.Nonesuch
Unbelievable, this should be upvoted more! Saved my day after 4 hours of swearing ^_^Blurt
this is a crazy fix - give it a try, fixed the issue for me as well!Bair
S
5

For anyone looking to just trigger a breakpoint in your app’s notification extension, the process is pretty simple in Xcode 11.3:

  1. Select the scheme of the extension (not the parent app scheme).
  2. Select Edit Scheme.
  3. Select the Executable to be your parent app.
  4. Uncheck Debug executable. By unchecking this, you are informing Xcode to debug the extension instead of the parent app. This is key to getting Xcode to stop within the extension’s breakpoints.
  5. Select Automatically under the Launch option.
  6. Run the scheme of the extension (not the parent app).
  7. Send your device a notification. Profit.

Unless some other stuff triggers Xcode’s moodiness, the above settings should ensure that whenever you select and run the extension scheme, the breakpoints within the extension will be hit. You can go back and check Debug executable in step 4 if you want Xcode to stop at breakpoints within the parent app.

Pro-tip: Don’t forget to add "mutable-content": 1 within the notification JSON payload, otherwise you will have a major sad (iOS won’t invoke the extension without that key).


Here’s a picture of my scheme editor dialog for the notification extension:

enter image description here

Szechwan answered 14/2, 2020 at 3:11 Comment(0)
T
4

After trying many of the possible fixes already given without success, it dawned on me that a framework had been mistakenly added to our extension target rather than the unit test target.

Removing the framework and targeting 10.2 allowed my extension to be called once again.

If you are curious as to which framework for whatever reason you can find it here: https://github.com/plu/JPSimulatorHacks

Thermonuclear answered 16/8, 2017 at 14:27 Comment(1)
Saved me from a big one, buddy. Thanks!Arcanum
Q
3

In my case it was what I forget to select NotificationServiceExtension in Scheme as particular app instead of "my application" where I try to use it. So I've been running "my app" and waited for breakpoints in code of another app (NotificationServiceExtension) and and that's why they never showed up. I tried every suggestions before that.

Quarterback answered 3/8, 2019 at 22:16 Comment(1)
I wasted four hours of my life until I found this solution. God bless you!Homo
W
3

After struggling with this I finally made this work for me by just changing 2 things.

  1. The bundleID of the NotificationServiceExtension target must be a different one. Preferred style com.companyname.appname.notificationservice(whatever). Setting the same bundleID of the app causes a failure in building to device.
  2. The main thing is the deployment target. I was double checking this with app's target but we must also check the deployment target of the newly created NotificationServiceExtenion's target which is by default the latest version. Set that to the minimum iOS version you would like to support but greater than ios 10.

Note: Make sure you have "mutable-content" : 1 in remote payload.

Hope this helps someone.

Wiggins answered 7/5, 2020 at 12:10 Comment(0)
S
2

The public func didReceiveNotificationRequest(request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) method in UNNotificationServiceExtension has changed between swift versions.

Some of the online examples are not up to date.

Make sure the method you're overriding in your custom subclass of UNNotificationServiceExtension

I had:

func didReceive(request: UNNotificationRequest, withContentHandler contentHandler:(UNNotificationContent) -> Void)

which didn't work until i changed to:

func didReceiveNotificationRequest(request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void)

Solander answered 4/10, 2016 at 19:4 Comment(12)
thanks for the comment. I just checked but I have the right method, Im using Objective-c, and I have exactly the same method it's in the interface (UNNotificationServiceExtension)Nephridium
Got it. Are you sending "category" in the push payload (server-side) ?Solander
probably no, we had the basic functionality for our APNS server, and we added what apple suggested only (mutable-content and any extra info we want to read on the client).Nephridium
I tried setting this category following apple specs (plist config and server side value too), but still not working. I see apple specs mention "NSExtensionAttribute" but lot of sample codes are using "NSExtensionAttributes" (plural), I tried both just in case, but I still have exactly the same result. it is very frustrating to have this error with no description at all of what is wrong, a simple message explaining what Apple is trying to valide would be enough to identify the issue...Nephridium
It's definitely NSExtensionAttributes can you post you relevant plist ?Solander
<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <string>attachmentCategory</string> <key>UNNotificationExtensionInitialContentSizeRatio</key> <real>1</real> </dict> <key>NSExtensionPointIdentifier</key> <string>com.apple.usernotifications.service</string> <key>NSExtensionPrincipalClass</key> <string>$(PRODUCT_MODULE_NAME).NotificationService</string> </dict>Nephridium
I added more info to my postNephridium
make sure you're sending category: attachmentCategory in your aps payloadSolander
yes, that's done, and I see is coming correctly to the device (there's a log showing aps data)Nephridium
is NSExtensionAttributes necessary? I'm able to get it to work on an development build without it.Limy
You'll be able to build without a problem, it's just that the push will not trigger the Notification Content ExtensionSolander
In Swift 5 (and maybe earlier), you need didReceive, otherwise you get the error Swift Compiler Error (Xcode): 'didReceiveNotificationRequest(_:withContentHandler:)' has been renamed to 'didReceive(_:withContentHandler:)'Epochmaking
M
2

My issue was two fold. The first probably was that I had set the Info.plist property NSExtensionPrincipalClass to bundle.identifier.NotificationService instead of ProductModuleName.NotificationService. Module name is the default, but I had erroneously changed to the identifier when debugging some other things related to different schemes and different targets.

The second issue was that I tested by running the notification service target. For me it worked much better when running the app target. I saw some other people recommending to use the notification service target to enable debugging. But that works fine when running the app target as well. You'll have to attach the debugger to your notification service manually though.

The way I discovered the above was to create a new test project with minimal code. In hindsight I definitely recommend that approach instead of trying out all the different solutions found on stackoverflow etc

Marileemarilin answered 15/12, 2017 at 11:1 Comment(0)
W
1

Be sure that you have 'None' configuration set. Look at the screenshot. With other parameters didReceive doesn't work.

Configuration

Willed answered 6/7, 2017 at 8:48 Comment(0)
C
1

Other possibility is to check category name in plist file of UNNotificationContentExtension.

String value of "UNNotificationExtensionCategory" in plist and "category/click_action" of payload should be same.

Catania answered 3/10, 2019 at 10:4 Comment(0)
Q
1

I'm currently working on Xcode 12.3 and Swift 5.2, and when I backed off the Main Project Target and Notification Service Extension Target from iOS 14.2 to iOS 13.2, it worked!

Quintillion answered 21/12, 2020 at 6:40 Comment(0)
N
0

The system executes your notification content app extension only when a remote notification’s payload contains the following information:

  • The payload must include the mutable-content key with a value of 1.

  • The payload must include an alert dictionary with title, subtitle, or body information.

Specifying the remote notification payload:

{
   “aps” : {
      “category” : “SECRET”,
      “mutable-content” : 1,
      “alert” : {
         “title” : “Secret Message!”,
         “body”  : “(Encrypted)”
     },
   },
   “ENCRYPTED_DATA” : “Salted__·öîQÊ$UDì_¶Ù∞è   Ω^¬%gq∞NÿÒQùw”
}
Neural answered 22/4, 2020 at 10:49 Comment(0)
A
0

Thanks for all good suggestions, which helped me fixed my mismatch with deployment target and missing mutable-content. But for me I still had issues getting the NSE to be called. I found the solution in the source code from this blog, https://medium.com/gits-apps-insight/processing-notification-data-using-notification-service-extension-6a2b5ea2da17. I was missing Embed App Extensions that copied the extension to my app. Why this was missing, I'm not sure, I have not seen this as a required step in the different tutorials I have read.

The Embed App Extension is just a "Copy File Phase" that has been renamed, as explained here https://mcmap.net/q/471962/-how-to-deal-with-xcode-quot-embed-app-extension-quot-build-phase

enter image description here

This tutorial was also very helpful setting up the different App Ids, App Groups, Profiles etc: https://blog.logrocket.com/implement-push-notifications-react-native-onesignal/

Aide answered 22/2, 2022 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.