Setup
Installation
Install via npm or yarn
npm i cleverpush-react-native -S
Install Pods for iOS
cd ios && pod install && cd ..
Setup iOS
Enable the required capabilities
Go to your root project and switch to the tab "Capabilities"
Enable "Push Notifications"
Enable "Background Modes" and check "Remote notifications"
Add Notification Service Extension
This is required for correctly tracking notification deliveries and for displaying big images or videos in notifications.
Select
File
>New
>Target
in XcodeChoose
Notification Service Extension
and pressNext
Enter
CleverPushNotificationServiceExtension
as Product Name, chooseObjective-C
as language and pressFinish
Press
Activate
on the next promptAdd the following at the bottom of your Project's Podfile
target 'CleverPushNotificationServiceExtension' do pod 'CleverPush' end
Add Notification Content Extension
This is required for displaying custom notification contents (e.g. Carousel Notifications).
Select
File
>New
>Target
in XcodeChoose
Notification Content Extension
and pressNext
Enter
CleverPushNotificationContentExtension
as Product Name, chooseObjective-C
as language and pressFinish
Press
Activate
on the next promptAdd the following at the bottom of your Project's Podfile
target 'CleverPushNotificationContentExtension' do pod 'CleverPush' end
Run
pod install
Open
CleverPushNotificationServiceExtension/NotificationService.m
and replace the whole content with the following:Objective-C:
#import <CleverPush/CleverPush.h> #import "NotificationService.h" @interface NotificationService () @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); @property (nonatomic, strong) UNNotificationRequest *receivedRequest; @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; @end @implementation NotificationService - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.receivedRequest = request; self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; [CleverPush didReceiveNotificationExtensionRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent]; self.contentHandler(self.bestAttemptContent); } - (void)serviceExtensionTimeWillExpire { [CleverPush serviceExtensionTimeWillExpireRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent]; self.contentHandler(self.bestAttemptContent); } @end
Swift:
import UserNotifications import CleverPush class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var receivedRequest: UNNotificationRequest! var bestAttemptContent: UNMutableNotificationContent? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.receivedRequest = request; self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) if let bestAttemptContent = bestAttemptContent { CleverPush.didReceiveNotificationExtensionRequest(self.receivedRequest, with: self.bestAttemptContent) contentHandler(bestAttemptContent) } } override func serviceExtensionTimeWillExpire() { if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { CleverPush.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent) contentHandler(bestAttemptContent) } } }
Open
CleverPushNotificationContentExtension/NotificationViewController.h
and replace the whole content with the following:Objective-C:
#import <UIKit/UIKit.h> #import <CleverPush/CleverPush.h> @interface NotificationViewController : CPNotificationViewController @end
Open
CleverPushNotificationContentExtension/NotificationViewController.m
and replace the whole content with the following:Objective-C:
#import "NotificationViewController.h" #import <UserNotifications/UserNotifications.h> #import <UserNotificationsUI/UserNotificationsUI.h> @interface NotificationViewController () <UNNotificationContentExtension> @end @implementation NotificationViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)didReceiveNotification:(UNNotification *)notification { [self cleverpushDidReceiveNotification:notification]; } - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion { [self cleverpushDidReceiveNotificationResponse:response completionHandler:completion]; } @end
Open
CleverPushNotificationContentExtension/Info.plist
and replace the whole content with the following:<?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>CFBundleDevelopmentRegion</key> <string>$(DEVELOPMENT_LANGUAGE)</string> <key>CFBundleDisplayName</key> <string>CleverPushNotificationContentExtension</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>$(PRODUCT_NAME)</string> <key>CFBundlePackageType</key> <string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleVersion</key> <string>1</string> <key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <array> <string>carousel</string> </array> <key>UNNotificationExtensionDefaultContentHidden</key> <false/> <key>UNNotificationExtensionInitialContentSizeRatio</key> <real>0.5</real> </dict> <key>NSExtensionPrincipalClass</key> <string>NotificationViewController</string> <key>NSExtensionPointIdentifier</key> <string>com.apple.usernotifications.content-extension</string> </dict> </dict> </plist>
Create your iOS push certificate
- Open Keychain Access on your Mac. (Application > Utilities > Keychain Access).
- Select Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority...
- Select the "Save to disk" option and enter any information in the required fields
- Go to the Apple developer portal, select your app and press "Edit"
- Enable "Push notifications" and press "Done"
- Go to the Create new certificate page, select "Apple Push Notification service SSL" and press "Continue"
- Select your Application Bundle ID and press "Continue"
- Press "Choose File...", select the previously generated "certSigningRequest" file and then press "Generate"
- Press "Download" and save your certificate
- Click on the downloaded .cer file, Keychain Access should open
- Select Login > My Certificates then right click on your key and click "Export (Apple Production iOS Push Services: com.your.bundle)..."
- Give the file a unique name and press save, be sure to leave the password field blank!
- Upload your certificate in the CleverPush channel settings
8. Add AppGroup (optional but recommended)
This is required for getting the received notifications via the `getNotifications` method and also for automatic Badge Counting (i.e. when using `setIncrementBadge(true)`).
1. Select your main application Target in Xcode
2. Go to `Capabilities` and activate `App Groups`
3. Create a new App Group with the following Scheme: `group.YOUR.BUNDLE.ID.cleverpush` (replace `YOUR.BUNDLE.ID` with your application's bundle identifier).
4. Enable the created App Group by checking the checkbox next to it
5. Select the `CleverPushNotificationServiceExtension` target and also enable the created App Group under `Capabilities`
Common iOS errors:
ld: library not found for -lcleverpush-react-native
Go in Xcode > Targets > Your App > Build Phases > Link Binary With Library. Click the + and select libcleverpush-react-native.a, then rebuild.
Setup Android
Setup Firebase inside your Android App and register the Sender ID and the API Key inside the CleverPush Channel settings. Please refer to the official docs: https://firebase.google.com/docs/android/setup
Comment or remove Expo's FCM listener (if it exists) from
android/app/src/AndroidManifest.xml
:<!-- FCM --> <service android:name=".fcm.ExpoFcmMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>