Skip to content

Commit

Permalink
Merge pull request #24 from quintonwall/master
Browse files Browse the repository at this point in the history
Support for registering with Salesforce Notification services
  • Loading branch information
mike4aday authored Feb 27, 2017
2 parents 2db67ee + f6a0406 commit 101ea4b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
57 changes: 56 additions & 1 deletion Example/SwiftlySalesforce/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import UIKit
import SwiftlySalesforce
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, LoginDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, LoginDelegate {

var window: UIWindow?

Expand All @@ -20,11 +21,65 @@ class AppDelegate: UIResponder, UIApplicationDelegate, LoginDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
configureSalesforce(consumerKey: consumerKey, redirectURL: redirectURL)

//uncomment if you want to receive push notifications, including those
//from salesforce's universal notification service
//registerForRemoteNotification()
return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
handleRedirectURL(url: url)
return true
}

//
//MARK: Push notifications
// This code below is only required if you care about push notifications.
//
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
salesforce.registerForSalesforceNotifications(devicetoken: deviceTokenString)
.then {
(result) -> () in
print("successfully registered for salesforce notifications")
}.catch {
error in
print(error)
}
}

//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}

//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("didReceive = ",response.notification.request.content.userInfo)

completionHandler()

}

func registerForRemoteNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
UIApplication.shared.applicationIconBadgeNumber = 0
}

}
11 changes: 11 additions & 0 deletions Pod/Classes/Salesforce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ open class Salesforce {
}
return request(requestBuilder: builder, jsonDeserializer: deserializer)
}

/// Use this method to register your device to receive push notifications from the Salesforce Universal Push Notification service
/// - Parameter devicetoken: the device token returned from a successful UIApplication.shared.registerForRemoteNotification() invocation.
/// - Returns: Promise of Any Type; result will either be success, or failure message
open func registerForSalesforceNotifications(devicetoken: String) -> Promise<Any> {
let headers = ["Content-Type" : "application/json"]
let params = ["ConnectionToken" : devicetoken, "ServiceType" : "Apple" ]
return custom(method: .post, path: "/services/data/v\(version)/sobjects/MobilePushServiceDevice", parameters: params, headers: headers)
}



fileprivate func request<T,U>(requestBuilder: @escaping (AuthData) throws -> URLRequest, jsonDeserializer: @escaping (U) throws -> T) -> Promise<T> {

Expand Down

0 comments on commit 101ea4b

Please sign in to comment.