Skip to content

Commit

Permalink
fix/Respond to push notification that launches application
Browse files Browse the repository at this point in the history
  • Loading branch information
wassil committed Jan 5, 2021
1 parent 6bbb812 commit 21432a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
13 changes: 8 additions & 5 deletions Documentation/Guide/PUSH_QUICKSTART.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,26 @@ To enable push notifications, configure the SDK with `pushNotificationTracking:

## 3. Application delegate methods
Application needs so to be able to respond to push notification related events.
You will need to setup 3 delegate methods:
You will need to setup 4 delegate methods:
- `application:didRegisterForRemoteNotificationsWithDeviceToken:` will be called when your application registers for push notifications.
- `application(_:didReceiveRemoteNotification:fetchCompletionHandler:)` will be called for silent push notifications and alert push notifications when your app is opened.
- `userNotificationCenter(_:didReceive:withCompletionHandler:)` will be called when user opens alert push notification.
- `application:didFinishLaunchingWithOptions` will be called with launch options containing a notification when push notification was opened while app wasn't running.

Exponea SDK contains `ExponeaAppDelegate` that contains implementation of these methods. Easiest way to integrate is make your `AppDelegate` extend `ExponeaAppDelegate`. If you don't want to/cannot use `ExponeaAppDelegate`, check implementation of those methods and call respective Exponea methods.

You have to call `UNUserNotificationCenter.current().delegate = self` to register last delegate method to Notification Center. Ideally, do so in `application:didFinishLaunchingWithOptions:`.

``` swift
@UIApplicationMain
class AppDelegate: ExponeaAppDelegate {
func application(
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
UNUserNotificationCenter.current().delegate = self
// don't forget to call the super method!!
super.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
Exponea.shared.checkPushSetup = true
Exponea.shared.configure(...)
}
Expand Down
6 changes: 3 additions & 3 deletions Documentation/PUSH.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import ExponeaSDK
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
class AppDelegate: ExponeaAppDelegate {

var window: UIWindow?

func application(
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
Exponea.shared.pushNotificationsDelegate = self
super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

Expand Down
9 changes: 6 additions & 3 deletions ExponeaSDK/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class AppDelegate: ExponeaAppDelegate {
var window: UIWindow?
var alertWindow: UIWindow?

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
super.application(application, didFinishLaunchingWithOptions: launchOptions)
Exponea.logger = AppDelegate.memoryLogger
Exponea.logger.logLevel = .verbose

Expand All @@ -39,7 +42,7 @@ class AppDelegate: ExponeaAppDelegate {
], intentIdentifiers: [], options: [])

UNUserNotificationCenter.current().setNotificationCategories([category1, category2])
UNUserNotificationCenter.current().delegate = self

return true
}

Expand Down
17 changes: 17 additions & 0 deletions ExponeaSDK/ExponeaSDK/Classes/ExponeaAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
import Foundation

open class ExponeaAppDelegate: NSObject, UNUserNotificationCenterDelegate, UIApplicationDelegate {
/// When the application is started by opening a push notification we need to get if from launch options.
/// When you override this method, don't forget to call
/// super.application(application, didFinishLaunchingWithOptions: launchOptions)
@discardableResult
open func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// process push notification from launch options - when app is not running while push is received
if let launchOptions = launchOptions,
let userInfo = launchOptions[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
Exponea.shared.handlePushNotificationOpened(userInfo: userInfo)
}
UNUserNotificationCenter.current().delegate = self
return true
}

open func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
Expand Down

0 comments on commit 21432a3

Please sign in to comment.