You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1.1 In Xcode Select File > New > Target...
1.2 Select Notification Service Extension then press Next
1.3 Enter the product name as OneSignalNotificationServiceExtension and press Finish.
1.4 Press Cancel on the Activate scheme prompt.
By cancelling, you are keeping Xcode debugging your app, instead of just the extension. If you activate by accident, you can always switch back to debug your app within Xcode (next to the play button).
1.5 In the project navigator, click the top level project directory to open the Xcode project settings and select the OneSignalNotificationServiceExtension target in the project and targets list. Unless you have a specific reason not to, you should set the Deployment Target to be iOS 10.
1.6 Open NotificationService.m or NotificationService.swift and replace the whole file contents with the below code:
`
import UserNotifications
import OneSignal
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var receivedRequest: UNNotificationRequest!
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.receivedRequest = request;
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: self.bestAttemptContent)
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 {
OneSignal.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
}`
Ignore any build errors at this point, step 2 will import OneSignal which will resolve any errors.
Import OneSignal intoRadio Swift Xcode project
Setup CocoaPods on your system if you don't have it already.
Alternatively follow our Carthage setup guide instead if you can't use Cocoapods in your project.
Make sure you have version 1.1.0 or newer by running pod --version from the terminal.
Run the following to upgrade sudo gem install cocoapods
2.1 Make sure your current Xcode project is closed.
2.2 Run pod init from the terminal in your project directory.
2.3 Open the newly created Podfile with your favorite code editor such as Sublime.
2.4 Add the OneSignal dependency under your project name target as well as OneSignalNotificationServiceExtension target like below:
`
target 'SwiftRadio' do
pod 'OneSignal', '>= 2.6.2', '< 3.0'
end
target 'OneSignalNotificationServiceExtension' do
pod 'OneSignal', '>= 2.6.2', '< 3.0'
end
`
run following terminal: pod repo update
then pod install
2.6 Open the newly created .xcworkspace file.
Make sure to always open the workspace from now on.
Add Required Capabilities
3.1 Select the root project and Under Capabilities Enable "Push Notifications".
Only do this for the main application target.
Do not do this for the Notification Service Extension.
3.2 Next Enable "Background Modes" and check "Remote notifications".
Add Required Code
Navigate to the AppDelegate file and add the following OneSignal initialization code to didFinishLaunchingWithOptions. Replace YOUR_APP_ID with your OneSignal App ID.
`
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
// Replace 'YOUR_APP_ID' with your OneSignal App ID.
OneSignal.initWithLaunchOptions(launchOptions,
appId: "YOUR_ONESIGNAL_APP_ID",
handleNotificationAction: nil,
settings: onesignalInitSettings)
// Recommend moving the below line to prompt for push after informing the user about
// how your app will use them.
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: (accepted)")
})
`
I added One Signal Push Notification and its working great:
Step:
0.1 Create One Signal Account
0.2 Generate iOS Push Certification for One Signal iOS Device.
https://documentation.onesignal.com/docs/generate-an-ios-push-certificate
1.1 In Xcode Select File > New > Target...
1.2 Select Notification Service Extension then press Next
1.3 Enter the product name as OneSignalNotificationServiceExtension and press Finish.
1.4 Press Cancel on the Activate scheme prompt.
By cancelling, you are keeping Xcode debugging your app, instead of just the extension. If you activate by accident, you can always switch back to debug your app within Xcode (next to the play button).
1.5 In the project navigator, click the top level project directory to open the Xcode project settings and select the OneSignalNotificationServiceExtension target in the project and targets list. Unless you have a specific reason not to, you should set the Deployment Target to be iOS 10.
1.6 Open NotificationService.m or NotificationService.swift and replace the whole file contents with the below code:
`
import UserNotifications
import OneSignal
class NotificationService: UNNotificationServiceExtension {
}`
Ignore any build errors at this point, step 2 will import OneSignal which will resolve any errors.
Setup CocoaPods on your system if you don't have it already.
Alternatively follow our Carthage setup guide instead if you can't use Cocoapods in your project.
2.1 Make sure your current Xcode project is closed.
2.2 Run pod init from the terminal in your project directory.
2.3 Open the newly created Podfile with your favorite code editor such as Sublime.
2.4 Add the OneSignal dependency under your project name target as well as OneSignalNotificationServiceExtension target like below:
`
target 'SwiftRadio' do
pod 'OneSignal', '>= 2.6.2', '< 3.0'
end
target 'OneSignalNotificationServiceExtension' do
pod 'OneSignal', '>= 2.6.2', '< 3.0'
end
`
run following terminal:
pod repo update
then
pod install
2.6 Open the newly created .xcworkspace file.
Make sure to always open the workspace from now on.
3.1 Select the root project and Under Capabilities Enable "Push Notifications".
3.2 Next Enable "Background Modes" and check "Remote notifications".
Navigate to the AppDelegate file and add the following OneSignal initialization code to didFinishLaunchingWithOptions. Replace YOUR_APP_ID with your OneSignal App ID.
`
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
// Replace 'YOUR_APP_ID' with your OneSignal App ID.
OneSignal.initWithLaunchOptions(launchOptions,
appId: "YOUR_ONESIGNAL_APP_ID",
handleNotificationAction: nil,
settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
// Recommend moving the below line to prompt for push after informing the user about
// how your app will use them.
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: (accepted)")
})
`
Make sure to import the OneSignal headers:
source:
https://documentation.onesignal.com/docs/ios-sdk-setup
The text was updated successfully, but these errors were encountered: