-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
ad4bfa0
commit 4a16b4c
Showing
36 changed files
with
1,088 additions
and
1 deletion.
There are no files selected for viewing
368 changes: 368 additions & 0 deletions
368
Examples/Manual Push Notifications/Manual Push Notifications.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
Examples/Manual Push Notifications/Manual Push Notifications/AppDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// AppDelegate.swift | ||
// Manual Push Notifications | ||
// | ||
// Created by Brian Boyle on 23/06/2023. | ||
// | ||
|
||
import UIKit | ||
import UserNotifications | ||
import Intercom | ||
|
||
let INTERCOM_APP_ID = "<#YOUR APP ID#>" | ||
let INTERCOM_API_KEY = "<#YOUR API KEY#>" | ||
|
||
@main | ||
/// Sample code to demonstrate how to manually handle Intercom Push Notifications. | ||
/// See our developer docs for more info https://developers.intercom.com/installing-intercom/docs/ios-push-notifications#manually | ||
/// | ||
/// In order to handle Intercom Push Notifications manually, please ensure that `IntercomAutoIntegratePushNotifications` is set to `NO` in your `Info.plist`. | ||
class AppDelegate: UIResponder, UIApplicationDelegate { | ||
|
||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | ||
/// ⚠️ Its importanr that the `UNUserNotificationCenterDelegate` is assigned a value here. | ||
/// This is to ensure that `didReceiveResponse` will be called when the app launches after tapping on a push notification. | ||
UNUserNotificationCenter.current().delegate = self | ||
loginToIntercom() | ||
registerForPushNotifications() | ||
return true | ||
} | ||
|
||
fileprivate func loginToIntercom() { | ||
Intercom.enableLogging() | ||
Intercom.setApiKey(INTERCOM_API_KEY, forAppId: INTERCOM_APP_ID) | ||
Intercom.setLauncherVisible(true) | ||
|
||
let defaults = UserDefaults.standard | ||
if let email = defaults.string(forKey: emailKey) { | ||
let attributes = ICMUserAttributes() | ||
attributes.email = email | ||
Intercom.loginUser(with: attributes) { result in | ||
switch result { | ||
case .success: print("Successfully logged in \(email)") | ||
case .failure(let error): print("Error logging in: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func registerForPushNotifications() { | ||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in | ||
print("Permission granted: \(granted)") | ||
} | ||
UIApplication.shared.registerForRemoteNotifications() | ||
} | ||
|
||
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | ||
/// Send your `deviceToken` to Intercom once you receive it from the system. | ||
Intercom.setDeviceToken(deviceToken) | ||
} | ||
|
||
// MARK: UISceneSession Lifecycle | ||
|
||
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { | ||
// Called when a new scene session is being created. | ||
// Use this method to select a configuration to create the new scene with. | ||
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) | ||
} | ||
} | ||
|
||
extension AppDelegate : UNUserNotificationCenterDelegate { | ||
|
||
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | ||
let userInfo = response.notification.request.content.userInfo | ||
|
||
/// Check if the push notification belongs to Intercom. If it does, pass it to Intercom to handle it. | ||
if Intercom.isIntercomPushNotification(userInfo) { | ||
Intercom.handlePushNotification(userInfo) | ||
} | ||
completionHandler() | ||
} | ||
} | ||
|
152 changes: 152 additions & 0 deletions
152
... Notifications/Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"size" : "20x20", | ||
"idiom" : "iphone", | ||
"filename" : "final_icon-42.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "20x20", | ||
"idiom" : "iphone", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
}, | ||
{ | ||
"size" : "29x29", | ||
"idiom" : "iphone", | ||
"filename" : "final_icon-29.png", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"size" : "29x29", | ||
"idiom" : "iphone", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "29x29", | ||
"idiom" : "iphone", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
}, | ||
{ | ||
"size" : "40x40", | ||
"idiom" : "iphone", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "40x40", | ||
"idiom" : "iphone", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"size" : "57x57", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"size" : "57x57", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "60x60", | ||
"idiom" : "iphone", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "60x60", | ||
"idiom" : "iphone", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
}, | ||
{ | ||
"size" : "20x20", | ||
"idiom" : "ipad", | ||
"filename" : "final_icon-20.png", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"size" : "20x20", | ||
"idiom" : "ipad", | ||
"filename" : "final_icon-43.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "29x29", | ||
"idiom" : "ipad", | ||
"filename" : "final_icon-30.png", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"size" : "29x29", | ||
"idiom" : "ipad", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "40x40", | ||
"idiom" : "ipad", | ||
"filename" : "final_icon-41.png", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"size" : "40x40", | ||
"idiom" : "ipad", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"size" : "50x50", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"size" : "50x50", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"size" : "72x72", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"size" : "72x72", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "76x76", | ||
"idiom" : "ipad", | ||
"filename" : "final_icon-76.png", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"size" : "76x76", | ||
"idiom" : "ipad", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "83.5x83.5", | ||
"idiom" : "ipad", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"size" : "1024x1024", | ||
"idiom" : "ios-marketing", | ||
"filename" : "appstore-icon.png", | ||
"scale" : "1x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+255 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/appstore-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.22 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/final_icon-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.28 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.07 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/final_icon-29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.34 KB
...al Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.34 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.72 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.07 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/final_icon-30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.34 KB
...al Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.34 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.73 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.49 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/final_icon-41.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.49 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/final_icon-42.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.49 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/final_icon-43.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.73 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.99 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.17 KB
.../Manual Push Notifications/Assets.xcassets/AppIcon.appiconset/final_icon-76.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.69 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.6 KB
...nual Push Notifications/Assets.xcassets/AppIcon.appiconset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions
6
Examples/Manual Push Notifications/Manual Push Notifications/Assets.xcassets/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ions/Manual Push Notifications/Assets.xcassets/Intercom-logo-white.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+2.25 KB
...cations/Assets.xcassets/Intercom-logo-white.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.05 KB
...cations/Assets.xcassets/Intercom-logo-white.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.2 KB
...cations/Assets.xcassets/Intercom-logo-white.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
...ifications/Manual Push Notifications/Assets.xcassets/Intercom-logo.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "intercom-grotesk (1).pdf", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+11.8 KB
...Manual Push Notifications/Assets.xcassets/Intercom-logo.imageset/intercom-grotesk (1).pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions
25
Examples/Manual Push Notifications/Manual Push Notifications/Info.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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>UIApplicationSceneManifest</key> | ||
<dict> | ||
<key>UIApplicationSupportsMultipleScenes</key> | ||
<false/> | ||
<key>UISceneConfigurations</key> | ||
<dict> | ||
<key>UIWindowSceneSessionRoleApplication</key> | ||
<array> | ||
<dict> | ||
<key>UISceneConfigurationName</key> | ||
<string>Default Configuration</string> | ||
<key>UISceneDelegateClassName</key> | ||
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string> | ||
<key>UISceneStoryboardFile</key> | ||
<string>Main</string> | ||
</dict> | ||
</array> | ||
</dict> | ||
</dict> | ||
</dict> | ||
</plist> |
56 changes: 56 additions & 0 deletions
56
Examples/Manual Push Notifications/Manual Push Notifications/LaunchScreen.storyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17154" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM"> | ||
<device id="retina5_9" orientation="portrait" appearance="light"/> | ||
<dependencies> | ||
<deployment identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17124"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<scenes> | ||
<!--View Controller--> | ||
<scene sceneID="EHf-IW-A2E"> | ||
<objects> | ||
<viewController id="01J-lp-oVM" sceneMemberID="viewController"> | ||
<layoutGuides> | ||
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/> | ||
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/> | ||
</layoutGuides> | ||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3"> | ||
<rect key="frame" x="0.0" y="0.0" width="375" height="812"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<subviews> | ||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Intercom-logo-white" translatesAutoresizingMaskIntoConstraints="NO" id="3CI-lR-kUZ"> | ||
<rect key="frame" x="137.66666666666666" y="356" width="100" height="100"/> | ||
<constraints> | ||
<constraint firstAttribute="width" constant="100" id="O3I-tC-6da"/> | ||
</constraints> | ||
</imageView> | ||
</subviews> | ||
<color key="backgroundColor" red="0.18823529411764706" green="0.27843137254901962" blue="0.92549019607843142" alpha="1" colorSpace="calibratedRGB"/> | ||
<constraints> | ||
<constraint firstItem="3CI-lR-kUZ" firstAttribute="top" secondItem="Llm-lL-Icb" secondAttribute="bottom" constant="194" id="CjD-jf-BoS"/> | ||
<constraint firstItem="3CI-lR-kUZ" firstAttribute="centerX" secondItem="Ze5-6b-2t3" secondAttribute="centerX" id="X53-aN-c7z"/> | ||
<constraint firstItem="3CI-lR-kUZ" firstAttribute="centerY" secondItem="Ze5-6b-2t3" secondAttribute="centerY" id="fnV-og-nuW"/> | ||
<constraint firstItem="3CI-lR-kUZ" firstAttribute="centerY" secondItem="Ze5-6b-2t3" secondAttribute="centerY" constant="-56" id="pfm-HF-cid"/> | ||
<constraint firstItem="3CI-lR-kUZ" firstAttribute="centerX" secondItem="Ze5-6b-2t3" secondAttribute="centerX" constant="-50" id="w4C-Zh-ATS"/> | ||
</constraints> | ||
<variation key="default"> | ||
<mask key="constraints"> | ||
<exclude reference="CjD-jf-BoS"/> | ||
<exclude reference="pfm-HF-cid"/> | ||
<exclude reference="w4C-Zh-ATS"/> | ||
</mask> | ||
</variation> | ||
</view> | ||
<nil key="simulatedStatusBarMetrics"/> | ||
<nil key="simulatedTopBarMetrics"/> | ||
</viewController> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/> | ||
</objects> | ||
<point key="canvasLocation" x="-44" y="381"/> | ||
</scene> | ||
</scenes> | ||
<resources> | ||
<image name="Intercom-logo-white" width="100" height="100"/> | ||
</resources> | ||
</document> |
Oops, something went wrong.