Skip to content

Commit

Permalink
Merge pull request #355 from CleverTap/develop
Browse files Browse the repository at this point in the history
Update Example App to support iOS Push Notification
  • Loading branch information
nishant-clevertap authored Sep 13, 2023
2 parents 8348417 + e48bfcf commit 15db9af
Show file tree
Hide file tree
Showing 19 changed files with 1,331 additions and 10 deletions.
928 changes: 927 additions & 1 deletion Example/ios/Example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Example/ios/Example/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
Expand Down Expand Up @@ -35,11 +36,21 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[CleverTap setDebugLevel:CleverTapLogDebug];
#endif
[CleverTap autoIntegrate];
[self addNotificationCategories];
[[CleverTapReactManager sharedInstance] applicationDidLaunchWithOptions:launchOptions];

return YES;
}

- (void)addNotificationCategories {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action_1" title:@"Back" options:UNNotificationActionOptionNone];
UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"action_2" title:@"Next" options:UNNotificationActionOptionNone];
UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action_3" title:@"View In App" options:UNNotificationActionOptionNone];
UNNotificationCategory *cat = [UNNotificationCategory categoryWithIdentifier:@"CTNotification" actions:@[action1, action2, action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
[center setNotificationCategories:[NSSet setWithObjects:cat, nil]];
}

// Deep links

- (BOOL)application:(UIApplication *)app
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="M4Y-Lb-cyx">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21678"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Notification View Controller-->
<scene sceneID="cwh-vc-ff4">
<objects>
<viewController id="M4Y-Lb-cyx" userLabel="Notification View Controller" customClass="NotificationViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" simulatedAppContext="notificationCenter" id="S3S-Oj-5AN">
<rect key="frame" x="0.0" y="0.0" width="320" height="37"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="2BE-c3-nQJ"/>
<color key="backgroundColor" red="0.45882353186607361" green="0.74901962280273438" blue="0.66666668653488159" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<extendedEdge key="edgesForExtendedLayout"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="320" height="37"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="vXp-U4-Rya" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="101" y="-3"/>
</scene>
</scenes>
</document>
24 changes: 24 additions & 0 deletions Example/ios/NotificationContent/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>UNNotificationExtensionCategory</key>
<string>CTNotification</string>
<key>UNNotificationExtensionInitialContentSizeRatio</key>
<real>0.1</real>
<key>UNNotificationExtensionUserInteractionEnabled</key>
<string>1</string>
<key>UNNotificationExtensionDefaultContentHidden</key>
<true/>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.content-extension</string>
</dict>
</dict>
</plist>
5 changes: 5 additions & 0 deletions Example/ios/NotificationContent/NotificationViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <CTNotificationContent/CTNotificationContent.h>

@interface NotificationViewController : CTNotificationViewController

@end
25 changes: 25 additions & 0 deletions Example/ios/NotificationContent/NotificationViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#import "NotificationViewController.h"

/*
Note: We have added two Notification Content target for Objective-C and Swift only for sample codes.
You can also use multiple Notification Content target at a time, only you need to add different
`UNNotificationExtensionCategory` for each content target as added here in both Info.plist file,
to show different category buttons added in AppDelegate file.
*/
@implementation NotificationViewController

- (void)viewDidLoad {
[super viewDidLoad];
}

// optional: implement to get user event type data
- (void)userDidPerformAction:(NSString *)action withProperties:(NSDictionary *)properties {
NSLog(@"user did perform action: %@ with props: %@", action , properties);
}

// optional: implement to get notification response
- (void)userDidReceiveNotificationResponse:(UNNotificationResponse *)response {
NSLog(@"user did receive notification response: %@:", response);
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="M4Y-Lb-cyx">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21678"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Notification View Controller-->
<scene sceneID="cwh-vc-ff4">
<objects>
<viewController id="M4Y-Lb-cyx" userLabel="Notification View Controller" customClass="NotificationViewController" customModule="NotificationContentSwift" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" simulatedAppContext="notificationCenter" id="S3S-Oj-5AN">
<rect key="frame" x="0.0" y="0.0" width="320" height="37"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="2BE-c3-nQJ"/>
<color key="backgroundColor" red="0.45882353186607361" green="0.74901962280273438" blue="0.66666668653488159" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<extendedEdge key="edgesForExtendedLayout"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="320" height="37"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="vXp-U4-Rya" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="139" y="4"/>
</scene>
</scenes>
</document>
24 changes: 24 additions & 0 deletions Example/ios/NotificationContentSwift/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>UNNotificationExtensionCategory</key>
<string>CTNotification1</string>
<key>UNNotificationExtensionInitialContentSizeRatio</key>
<real>0.1</real>
<key>UNNotificationExtensionUserInteractionEnabled</key>
<string>1</string>
<key>UNNotificationExtensionDefaultContentHidden</key>
<true/>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.content-extension</string>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import CTNotificationContent

/*
Note: We have added two Notification Content target for Objective-C and Swift only for sample codes.
You can also use multiple Notification Content target at a time, only you need to add different
`UNNotificationExtensionCategory` for each content target as added here in both Info.plist file,
to show different category buttons added in AppDelegate file.
*/
class NotificationViewController: CTNotificationViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
17 changes: 17 additions & 0 deletions Example/ios/NotificationService/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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>CleverTapAccountID</key>
<string>RKW-R77-KW6Z</string>
<key>CleverTapToken</key>
<string>aa2-032</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.service</string>
<key>NSExtensionPrincipalClass</key>
<string>NotificationService</string>
</dict>
</dict>
</plist>
5 changes: 5 additions & 0 deletions Example/ios/NotificationService/NotificationService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <CTNotificationService/CTNotificationService.h>

@interface NotificationService : CTNotificationServiceExtension

@end
25 changes: 25 additions & 0 deletions Example/ios/NotificationService/NotificationService.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#import "NotificationService.h"
#import <CleverTap-iOS-SDK/CleverTap.h>

/*
Note: We have added two Notification Service target for Objective-C and Swift only for sample codes.
You can activate/deactivate other target to use any one at a time.
Steps to add/remove target: Click on Example target -> Build Phases -> Target Dependencies -> Click on add/remove items.
*/
@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
// Add CleverTap Account ID and Account token in your target .plist file
[CleverTap setDebugLevel:2];
NSDictionary *profile = @{
@"Name": @"testUserA1",
@"Identity": @123456,
@"Email": @"[email protected]"
};
[[CleverTap sharedInstance] profilePush:profile];
[[CleverTap sharedInstance] recordNotificationViewedEventWithData: request.content.userInfo];

[super didReceiveNotificationRequest:request withContentHandler:contentHandler];
}

@end
17 changes: 17 additions & 0 deletions Example/ios/NotificationServiceSwift/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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>CleverTapAccountID</key>
<string>RKW-R77-KW6Z</string>
<key>CleverTapToken</key>
<string>aa2-032</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.service</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).NotificationService</string>
</dict>
</dict>
</plist>
19 changes: 19 additions & 0 deletions Example/ios/NotificationServiceSwift/NotificationService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import CTNotificationService
import CleverTapSDK

/*
Note: We have added two Notification Service target for Objective-C and Swift only for sample codes.
You can activate/deactivate other target to use any one at a time.
Steps to add/remove target: Click on Example target -> Build Phases -> Target Dependencies -> Click on add/remove items.
*/
class NotificationService: CTNotificationServiceExtension {
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
let profile: Dictionary<String, AnyObject> = [
"Name": "testUserA1" as AnyObject,
"Identity": 123456 as AnyObject,
"Email": "[email protected]" as AnyObject ]
CleverTap.sharedInstance()?.profilePush(profile)
CleverTap.sharedInstance()?.recordNotificationViewedEvent(withData: request.content.userInfo)
super.didReceive(request, withContentHandler: contentHandler)
}
}
19 changes: 19 additions & 0 deletions Example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ target 'Example' do
end
end
end

target 'NotificationService' do
pod "CleverTap-iOS-SDK", :modular_headers => true
pod "CTNotificationService", :modular_headers => true
end

target 'NotificationContent' do
pod "CTNotificationContent"
end

# Added these target for sample code only in Swift
target 'NotificationServiceSwift' do
pod "CleverTap-iOS-SDK", :modular_headers => true
pod "CTNotificationService", :modular_headers => true
end

target 'NotificationContentSwift' do
pod "CTNotificationContent"
end
2 changes: 1 addition & 1 deletion Example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@react-navigation/drawer": "^6.6.2",
"@react-navigation/native": "^6.1.6",
"@react-navigation/native-stack": "^6.9.12",
"clevertap-react-native": "git+https://github.com/CleverTap/clevertap-react-native#task/SDK-3168/update_android_sdk_to_5.2.0",
"clevertap-react-native": "git+https://github.com/CleverTap/clevertap-react-native#develop",
"react": "18.2.0",
"react-native": "0.71.8",
"react-native-drawer": "^2.5.1",
Expand Down
Loading

0 comments on commit 15db9af

Please sign in to comment.