Accessing App code from Notification Service Extension
Asked Answered
C

2

10

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.

Chairborne answered 21/5, 2019 at 20:59 Comment(4)
Have you ever tried this? I've never seen anything like this in documentation or examples. I was wondering too.Schrecklichkeit
I've tried to access the app code unfortunately failed.Chairborne
You would need to add the relevant source files to your extension target in XcodeBobbe
Not possible, until you add the relevant files to your service extensionEnamel
M
6

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.

Monospermous answered 27/5, 2020 at 13:29 Comment(1)
I am accessing Application's User Defaults Data and looping over that in my extension, but its not working for me. @MonospermousWight
B
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.

Brooking answered 27/5, 2020 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.