Skip to content

Commit 4a16b4c

Browse files
authored
Sample Code for Handling Push Notifications Manually (#417)
* Sample Code for Handling Push Notifications Manually - Added sample code that demonstrates how to handle push notifications manually * Update README.md
1 parent ad4bfa0 commit 4a16b4c

36 files changed

+1088
-1
lines changed

Examples/Manual Push Notifications/Manual Push Notifications.xcodeproj/project.pbxproj

Lines changed: 368 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//
2+
// AppDelegate.swift
3+
// Manual Push Notifications
4+
//
5+
// Created by Brian Boyle on 23/06/2023.
6+
//
7+
8+
import UIKit
9+
import UserNotifications
10+
import Intercom
11+
12+
let INTERCOM_APP_ID = "<#YOUR APP ID#>"
13+
let INTERCOM_API_KEY = "<#YOUR API KEY#>"
14+
15+
@main
16+
/// Sample code to demonstrate how to manually handle Intercom Push Notifications.
17+
/// See our developer docs for more info https://developers.intercom.com/installing-intercom/docs/ios-push-notifications#manually
18+
///
19+
/// In order to handle Intercom Push Notifications manually, please ensure that `IntercomAutoIntegratePushNotifications` is set to `NO` in your `Info.plist`.
20+
class AppDelegate: UIResponder, UIApplicationDelegate {
21+
22+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
23+
/// ⚠️ Its importanr that the `UNUserNotificationCenterDelegate` is assigned a value here.
24+
/// This is to ensure that `didReceiveResponse` will be called when the app launches after tapping on a push notification.
25+
UNUserNotificationCenter.current().delegate = self
26+
loginToIntercom()
27+
registerForPushNotifications()
28+
return true
29+
}
30+
31+
fileprivate func loginToIntercom() {
32+
Intercom.enableLogging()
33+
Intercom.setApiKey(INTERCOM_API_KEY, forAppId: INTERCOM_APP_ID)
34+
Intercom.setLauncherVisible(true)
35+
36+
let defaults = UserDefaults.standard
37+
if let email = defaults.string(forKey: emailKey) {
38+
let attributes = ICMUserAttributes()
39+
attributes.email = email
40+
Intercom.loginUser(with: attributes) { result in
41+
switch result {
42+
case .success: print("Successfully logged in \(email)")
43+
case .failure(let error): print("Error logging in: \(error.localizedDescription)")
44+
}
45+
}
46+
}
47+
}
48+
49+
func registerForPushNotifications() {
50+
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
51+
print("Permission granted: \(granted)")
52+
}
53+
UIApplication.shared.registerForRemoteNotifications()
54+
}
55+
56+
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
57+
/// Send your `deviceToken` to Intercom once you receive it from the system.
58+
Intercom.setDeviceToken(deviceToken)
59+
}
60+
61+
// MARK: UISceneSession Lifecycle
62+
63+
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
64+
// Called when a new scene session is being created.
65+
// Use this method to select a configuration to create the new scene with.
66+
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
67+
}
68+
}
69+
70+
extension AppDelegate : UNUserNotificationCenterDelegate {
71+
72+
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
73+
let userInfo = response.notification.request.content.userInfo
74+
75+
/// Check if the push notification belongs to Intercom. If it does, pass it to Intercom to handle it.
76+
if Intercom.isIntercomPushNotification(userInfo) {
77+
Intercom.handlePushNotification(userInfo)
78+
}
79+
completionHandler()
80+
}
81+
}
82+
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"images" : [
3+
{
4+
"size" : "20x20",
5+
"idiom" : "iphone",
6+
"filename" : "final_icon-42.png",
7+
"scale" : "2x"
8+
},
9+
{
10+
"size" : "20x20",
11+
"idiom" : "iphone",
12+
"filename" : "[email protected]",
13+
"scale" : "3x"
14+
},
15+
{
16+
"size" : "29x29",
17+
"idiom" : "iphone",
18+
"filename" : "final_icon-29.png",
19+
"scale" : "1x"
20+
},
21+
{
22+
"size" : "29x29",
23+
"idiom" : "iphone",
24+
"filename" : "[email protected]",
25+
"scale" : "2x"
26+
},
27+
{
28+
"size" : "29x29",
29+
"idiom" : "iphone",
30+
"filename" : "[email protected]",
31+
"scale" : "3x"
32+
},
33+
{
34+
"size" : "40x40",
35+
"idiom" : "iphone",
36+
"filename" : "[email protected]",
37+
"scale" : "2x"
38+
},
39+
{
40+
"size" : "40x40",
41+
"idiom" : "iphone",
42+
"filename" : "[email protected]",
43+
"scale" : "3x"
44+
},
45+
{
46+
"idiom" : "iphone",
47+
"size" : "57x57",
48+
"scale" : "1x"
49+
},
50+
{
51+
"idiom" : "iphone",
52+
"size" : "57x57",
53+
"scale" : "2x"
54+
},
55+
{
56+
"size" : "60x60",
57+
"idiom" : "iphone",
58+
"filename" : "[email protected]",
59+
"scale" : "2x"
60+
},
61+
{
62+
"size" : "60x60",
63+
"idiom" : "iphone",
64+
"filename" : "[email protected]",
65+
"scale" : "3x"
66+
},
67+
{
68+
"size" : "20x20",
69+
"idiom" : "ipad",
70+
"filename" : "final_icon-20.png",
71+
"scale" : "1x"
72+
},
73+
{
74+
"size" : "20x20",
75+
"idiom" : "ipad",
76+
"filename" : "final_icon-43.png",
77+
"scale" : "2x"
78+
},
79+
{
80+
"size" : "29x29",
81+
"idiom" : "ipad",
82+
"filename" : "final_icon-30.png",
83+
"scale" : "1x"
84+
},
85+
{
86+
"size" : "29x29",
87+
"idiom" : "ipad",
88+
"filename" : "[email protected]",
89+
"scale" : "2x"
90+
},
91+
{
92+
"size" : "40x40",
93+
"idiom" : "ipad",
94+
"filename" : "final_icon-41.png",
95+
"scale" : "1x"
96+
},
97+
{
98+
"size" : "40x40",
99+
"idiom" : "ipad",
100+
"filename" : "[email protected]",
101+
"scale" : "2x"
102+
},
103+
{
104+
"idiom" : "ipad",
105+
"size" : "50x50",
106+
"scale" : "1x"
107+
},
108+
{
109+
"idiom" : "ipad",
110+
"size" : "50x50",
111+
"scale" : "2x"
112+
},
113+
{
114+
"idiom" : "ipad",
115+
"size" : "72x72",
116+
"scale" : "1x"
117+
},
118+
{
119+
"idiom" : "ipad",
120+
"size" : "72x72",
121+
"scale" : "2x"
122+
},
123+
{
124+
"size" : "76x76",
125+
"idiom" : "ipad",
126+
"filename" : "final_icon-76.png",
127+
"scale" : "1x"
128+
},
129+
{
130+
"size" : "76x76",
131+
"idiom" : "ipad",
132+
"filename" : "[email protected]",
133+
"scale" : "2x"
134+
},
135+
{
136+
"size" : "83.5x83.5",
137+
"idiom" : "ipad",
138+
"filename" : "[email protected]",
139+
"scale" : "2x"
140+
},
141+
{
142+
"size" : "1024x1024",
143+
"idiom" : "ios-marketing",
144+
"filename" : "appstore-icon.png",
145+
"scale" : "1x"
146+
}
147+
],
148+
"info" : {
149+
"version" : 1,
150+
"author" : "xcode"
151+
}
152+
}
255 KB
Loading
2.22 KB
Loading
4.28 KB
Loading
2.07 KB
Loading
3.34 KB
Loading
3.34 KB
Loading
4.72 KB
Loading

0 commit comments

Comments
 (0)