6

Notification Service Extension modifies the content of a remote notification before it's delivered to the user. For example if a remote notification contains an image URL, Notification Service Extension can be used to fetch the image and show it in the notification content.

But how can I access actual app code in Notification Service Extension? Let's say I have a DataAccess swift class in app code, can I access this class in Notification Service Extension?

Edit: Some folks suggested to add app code to service extension, which is not applicable in my situation.

ramazan polat
  • 7,111
  • 1
  • 48
  • 76

2 Answers2

3

Go to the classes that you want to use in your app project. On your File inspector your are gonna see the Target Membership of your class. Check the extension's name checkbox. Then you can reuse your code.

You can also add the classes you need in the Build Phases -> Compiled Sources in your notification service target. The result is the same.

alecnash
  • 1,750
  • 1
  • 18
  • 42
  • I am accessing Application's User Defaults Data and looping over that in my extension, but its not working for me. @alecnash – Raja Saad Sep 19 '21 at 14:33
2

The usual solution is to create a library/framework which includes your DataAccess class, and your app and the Notification Service Extension uses this library/framework. Make sure you select Do not embed for your framework when adding to Notification Service Extension, and select Embed and sign when adding it to your app.

BoygeniusDexter
  • 2,154
  • 1
  • 16
  • 14