Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add One Signal Push Notification (Works 100%) #117

Open
LuckyMann opened this issue Apr 13, 2019 · 2 comments
Open

Add One Signal Push Notification (Works 100%) #117

LuckyMann opened this issue Apr 13, 2019 · 2 comments

Comments

@LuckyMann
Copy link

LuckyMann commented Apr 13, 2019

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. Add Notification Service Extension

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.

  1. 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.

  1. 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".

  1. 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)

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:

Swift: import OneSignal

source:
https://documentation.onesignal.com/docs/ios-sdk-setup

@LuckyMann LuckyMann changed the title Add One Signal Push Notification (Works) Add One Signal Push Notification (Works 100%) Apr 14, 2019
@losgranos
Copy link

confirmed

@Tucker2015
Copy link

+1 Confirmed working in Single App

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants