Skip to content

Commit

Permalink
feat: APNS Token API
Browse files Browse the repository at this point in the history
* Adding APNSToken feature

* Exposing APNS feature in SDK

* Registering APNS token in addition to push subscription

* Adding empty entitlement file to example app, so device token is generated

* Registering for push notifications in Example app

* Adding example for handling a tap on a notification

* Revert API Key and example user

* Adding Deletion of APNS Tokens on logout

* Removing old Push Subscription code
  • Loading branch information
stigi authored Apr 29, 2024
1 parent fe11224 commit 7afba6c
Show file tree
Hide file tree
Showing 20 changed files with 474 additions and 362 deletions.
4 changes: 4 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
911386F4690B2C7C97254425 /* Pods-Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example.release.xcconfig"; path = "Target Support Files/Pods-Example/Pods-Example.release.xcconfig"; sourceTree = "<group>"; };
94E508EE2BC71087002995F3 /* String+Empty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Empty.swift"; sourceTree = "<group>"; };
942497082BC1F76A006A3D3A /* NotificationPreferencesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationPreferencesViewController.swift; sourceTree = "<group>"; };
94AB99682BD6BDE000EC4797 /* Example.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Example.entitlements; sourceTree = "<group>"; };
94C074CD2BC212C5001615CF /* NotificationChannelsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationChannelsViewController.swift; sourceTree = "<group>"; };
D2DC15F52758C68000282C27 /* UIColorExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorExtensions.swift; sourceTree = "<group>"; };
D2DC15F72758CC0B00282C27 /* MagicBellStoreCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicBellStoreCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -98,6 +99,7 @@
59EF9A9C2744700800FB2378 /* Assets.xcassets */,
59EF9A9E2744700800FB2378 /* LaunchScreen.storyboard */,
59EF9AA12744700800FB2378 /* Info.plist */,
94AB99682BD6BDE000EC4797 /* Example.entitlements */,
);
path = Example;
sourceTree = "<group>";
Expand Down Expand Up @@ -435,6 +437,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 9YHGX7ST8H;
Expand Down Expand Up @@ -466,6 +469,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 9YHGX7ST8H;
Expand Down
51 changes: 50 additions & 1 deletion Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import UserNotifications
extension MagicBellClient {
/// Application global instance of MagicBellClient
static var shared = MagicBellClient(
apiKey: "d75bb407731e0be07547ad7d2f1c3aae3ebc1ec1",
apiKey: "34ed17a8482e44c765d9e163015a8d586f0b3383",
logLevel: .debug
)
}
Expand All @@ -29,6 +29,28 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

// Registering for push notification
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().getNotificationSettings { settings in
DispatchQueue.main.async {
if settings.authorizationStatus == .notDetermined {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { granted, error in
if let error = error {
print("Request Authorization Failed (\(error), \(error.localizedDescription))")
}
if granted {
UIApplication.shared.registerForRemoteNotifications()
}
}
} else if settings.authorizationStatus == .denied {
//
} else if settings.authorizationStatus == .authorized {
UIApplication.shared.registerForRemoteNotifications()
}
}
}

return true
}

Expand All @@ -46,9 +68,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Request Authorization Failed (\(error), \(error.localizedDescription))")
}


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Storing device token when refreshed
MagicBellClient.shared.setDeviceToken(deviceToken: deviceToken)
}
}

extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
print("Clicked on Push notification")
print(response.notification.request.content.userInfo)

let userInfo = response.notification.request.content.userInfo as NSDictionary
let title = userInfo.value(forKeyPath: "aps.alert.title") as? String ?? "Sent without title"
let body = userInfo.value(forKeyPath: "aps.alert.body") as? String ?? "Sent without body"
let alert = UIAlertController(title: "Notification opened",
message: "\(title)\n\(body)",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

UIApplication
.shared
.connectedScenes
.compactMap { ($0 as? UIWindowScene)?.keyWindow }
.last?.rootViewController?.present(alert, animated: true)
}
}
8 changes: 8 additions & 0 deletions Example/Example/Example.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Example/Example/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
window = UIWindow(windowScene: scene)

// Defining the user to test
let user = MagicBellClient.shared.connectUser(email: "hi@ullrich.is")
let user = MagicBellClient.shared.connectUser(email: "richard@example.com")

switch style {
case .uiKit:
Expand Down
Loading

0 comments on commit 7afba6c

Please sign in to comment.