Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@
// Created by Meena Alfons on 13/12/2023.
//

import os
import BirdKit
import Foundation
import UIKit

import BirdKit
import os

class AppDelegate: NSObject, UIApplicationDelegate {

static var bird: Bird!
static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "AppDelegate")

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
do {
Self.bird = try Bird()
} catch {
Self.logger.info("failed to create bird \(error)")
}

UNUserNotificationCenter.current().delegate = self
return true
}

// This method will be called from Button "Ask for notifications permission"
static func requestNotificationsPermission() {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { granted, error in
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) {
granted, error in
guard granted else {
self.logger.log("Notification permission is not gratned")
return
Expand All @@ -40,35 +43,38 @@ class AppDelegate: NSObject, UIApplicationDelegate {
}

}

func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
Self.bird.notifications.registerDevice(deviceToken: deviceToken)

Self.logger.log("APNS token: \(deviceToken.map { String(format: "%02x", $0) }.joined())")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

func application(
_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error
) {
Self.logger.log(#function)
}



func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
Self.bird.notifications.handleBackgroundNotification(userInfo: userInfo, completionHandler: completionHandler)
Self.bird.notifications.handleBackgroundNotification(
userInfo: userInfo, completionHandler: completionHandler)
}
}

extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) ->
Void
) {
Self.logger.log(#function)
let options = Self.bird.notifications.willPresentNotification(notification)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Created by Meena Alfons on 12/12/2023.
//

import SwiftUI
import BirdKit
import SwiftUI

struct ContentView: View {
@State var sourceUrl = ""
Expand All @@ -16,7 +16,7 @@ struct ContentView: View {
if sourceUrl != "" {
Text("Opened from \(sourceUrl)")
}

let signedIdentity = "change-me-with-sample-signed-identity"
Text("Push notifications")
Button("Ask for notifications permission") {
Expand All @@ -29,7 +29,8 @@ struct ContentView: View {
)
}
Button("Set Signed Identity") {
let signedIdentity = "change-me-with-sample-signed-identity"
let signedIdentity =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Im1ybjp2MTphcHBsaWNhdGlvbjppZGVudGl0eS1jbGFpbXMtaXNzdWVyOjAxZWNlODAxLWVmNTctNGQwNC1hZGFlLWJjN2U0YWQwOGFmZS8yZmQ5NTdlOS1jNTQwLTRhM2UtYjA5My0wZWE5NmU3NDY1ZjY6MSJ9.eyJpZGVudGlmaWVycyI6W3sia2V5IjoiZW1haWxhZGRyZXNzIiwidmFsdWUiOiJtZWVuYS50ZXN0MkBlbWFpbC5jb20ifV19.in94rLJe1X_7MejgnmV8f284Z8YPxRZcJV8LG5cTbVk"
AppDelegate.bird?.contact.identify(
signedIdentity: signedIdentity
)
Expand All @@ -40,10 +41,12 @@ struct ContentView: View {
}
Text("Contact")
Button("Put Attributes") {
let attributes = BirdKit.Attributes().put("displayName", "iOS: push notifications example")
AppDelegate.bird?.contact.putAttributes(attributes: attributes )
let attributes = BirdKit.Attributes()
.put("displayName", "iOS: push notifications example")
.put("subscribedPush", true)
AppDelegate.bird?.contact.putAttributes(attributes: attributes)
}

}
.padding()
.onOpenURL(perform: { url in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
@main
struct PushNotificationsExampleApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
WindowGroup {
ContentView()
Expand Down