Mobile SDK end-of-life has been announced and scheduled for the end of 2023, in favor of updated and more streamlined app personalization using server-side APIs.
Before you can create a Push Notification, the feature must be enabled by your Technical Account Manager. After the feature is enabled, you must implement the following technical procedure in your iOS or Android environment before you can create your first Push Notification.
- This procedure assumes that Dynamic Yield has been fully implemented for mobile and validated.
- If you want to report the user’s cart content to Dynamic Yield (recommended), use one of the following options:
- Perform one or both procedures below depending on the supported mobile operating systems of your application.
Note that for iOS, the certificate issued by Apple is valid for 12 months. Plan in advance to update it with the help of Dynamic Yield support.
Configuring Push Notifications for iOS
This procedure was verified on January 18th, 2018. Changes made by Apple after this date may affect this procedure.
- Enable Push Notifications for App in the Apple Dev Center & send the push certificate (p12 file) to Dynamic Yield.
- Login to Apple Dev Center and select ‘Navigate to Certificates, Identifiers & Profiles’.
- Edit your App ID and enable ‘Push Notifications’.
- Create APNS certificates for your development and production build by selecting ‘Create Certificate’ and following the instructions. Make sure to download the generated certificates.
- Open the downloaded certificate files. This adds it to your Keychain application.
- In the Keychain application, export the APNS certificates into p12 files. Entering a password is not mandatory. If you choose to use a password, remember it because you must send it to Dynamic Yield.
- Send the APNS certificates as p12 file to Dynamic Yield (along with the password you used when exporting it). The production APNS certificate should be sent for the Dynamic Yield production section. Sending the development APNS certificate is only needed is a Dynamic Yield dev section exists.
- Configure push notifications in the project
- In your app Xcode project, go to project settings
- Select the correct target
- In the ‘Capabilities’ tab, find “Push Notifications’ and change the toggle to ON
- Add ‘Notification Content Extension’.
This allows Dynamic Yield to render rich notifications in expanded mode. For more information, see UNNotification Content Extension Apple Developers.- In your app’s Xcode project, create a new target by selecting File › New Target.
- Select ‘Notification Content Extension’ and click Next, on the next screen enter the Product Name and click on Finish. A content extension folder (with the supplied Product Name) is added to your project.
- In the ‘General’ tab, change the ‘Deployment Target’ to ‘10.0’. This ensures that push Notifications are presented on all devices running iOS 10 and up.
- Add the Dynamic Yield SDK to ‘Linked Frameworks and Libraries’. The framework location depends on how your SDK is integrated.
Click + and perform one of the following steps:- If the Dynamic Yield SDK was integrated manually, search and select it from the ‘Workspace’ folder
- If the Dynamic Yield SDK was integrated using Cocoapods, select ‘Add Other..’, select the framework from your pods folder; usually the path is:
“…/Pods/Dynamic-Yield-iOS-SDK/DYApi/DYSDK.xcframework” - If the Dynamic Yield SDK was integrated using Carthage, select ‘Add Other..’, select the framework from your pods folder; usually the path is:
“…/Carthage/Build/iOS/DYSDK.xcframework”
- In the ‘Build Settings’ tab, search for “APPLICATION_EXTENSION_API_ONLY” and change it to ‘No’.
- Add the indicated lines to the NotificationViewController.m
#import <DYSDK/DYNotificationContent.h> //add this line @interface NotificationViewController () @property (strong,atomic) DYNotificationContent* dyContent; //add this line @end @implementation NotificationViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveNotification:(UNNotification *)notification { _dyContent = [[DYNotificationContent alloc] init]; //add all content from here to the end [_dyContent didReceiveNotification:notification withView:self.view]; } - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion{ [_dyContent didReceiveNotificationResponse:response completionHandler:completion]; } @end
- In order to allow action buttons in the Dynamic Yield push notification templates, edit the info.plist file as follows:
- Change the value type of key: UNNotificationExtensionCategory from String to Array
- Add the following items to the array:
- DYNextCheckout
- DYNextNone
- DYViewCheckout
- DYViewNone
- DYCheckoutNone
- DYNoneNone
- DYAddToCartView
- DYNextViewProd
- DYNextAddToCart
- Note: The button text is shown in English as default, but can be automatically translated to Russian if:
- The app supports Russian.
- The iOS device language is set to Russian.
- Add the ‘Notification Service Extension’
This allows Dynamic Yield to track engagement with the push notifications and render a thumbnail image in the push notification collapsed mode. For more information, see UNNotification Service Extension Apple Developers.- In your app’s Xcode project, create a new target by selecting File › New Target.
- Select ‘Notification Service Extension’ and click Next. On the next screen, enter the Product Name and click Finish. A service extension folder (with the supplied Product Name) is added to your project. The next three steps are similar to the content extension configuration.
- In the ‘General’ tab, change the ‘Deployment Target’ to ‘10.0’. This ensures that push Notifications are presented on all devices running iOS 10 and up.
- Add the Dynamic Yield SDK to ‘Linked Frameworks and Libraries’. The framework location depends on how your SDK is integrated.
Click + and perform one of the following steps:- If the Dynamic Yield SDK was integrated manually, search and select it from the ‘Workspace’ folder
- If the Dynamic Yield SDK was integrated using Cocoapods, select ‘Add Other..’, select the framework from your pods folder; usually the path is: “…/Pods/Dynamic-Yield-iOS-SDK/DYApi/DYSDK.xcframework”
- If the Dynamic Yield SDK was integrated using Carthage, select ‘Add Other..’, select the framework from your pods folder; usually the path is: “…/Carthage/Build/iOS/DYSDK.xcframework”
- In the ‘Build Settings’ tab, search for “APPLICATION_EXTENSION_API_ONLY” and change it to ‘No’.
- Add the indicated code to the NotificationService.m
#import <DYSDK/DYNotificationService.h> //add this line @interface NotificationService () @property (strong,atomic) DYNotificationService* dyService; //add this line @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); @property (nonatomic, strong) UNNotificationContent *originalContent; @end@implementation NotificationService - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.originalContent = request.content; _dyService = [[DYNotificationService alloc] init]; //add this line [_dyService didReceiveNotificationRequest:request withContentHandler:contentHandler]; //add this line } - (void)serviceExtensionTimeWillExpire { self.contentHandler(self.originalContent); }@end
- User push notifications registration handling
This requests the user’s permission to receive push notification from the app and to get the user’s Push ID.- Request permission to receive push notifications from the app by calling the following code in your app’s AppDelegate method application:didFinishLaunchingWithOptions
This causes iOS to display a popup requesting permission from the user immediately after launching the app. In case you’d like this permission request to be displayed at another time (for example, after presenting your own custom overlay explaining the value of push notifications), you can call the above method anywhere else in your app logic.[[DYApi getInstance] registerForPushNotifications];
- Pass the user’s device push token (PushID) to Dynamic YieldThis is done in method:application:didRegisterForRemoteNotificationsWithDeviceToken.If this is already implemented in your app, add the following code to the implementation:
Otherwise, add this method to your app’s AppDelegate class:[[DYApi getInstance] setPushNotificationID:deviceToken];
-(void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData*)deviceToken{ [[DYApi getInstance] setPushNotificationID:deviceToken]; }
- In case your app includes logic enabling users to subscribe and unsubscribe from push notifications, use the following methods to notify Dynamic Yield:
- Subscribe: subscribeToPushNotifications
- Unsubscribe: unsubscribeFromPushNotifications
- Request permission to receive push notifications from the app by calling the following code in your app’s AppDelegate method application:didFinishLaunchingWithOptions
-
Report push notification user clicks
This notifies Dynamic Yield of user clicks anywhere in the push notification, enabling our reporting metrics and attribution logic. This is handled differently in iOS versions. Both steps need to be added.
- Add the following to your app’s AppDelegate header file to add two macros that determine the device’s iOS version and imports Dynamic Yield’s tracking class:
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #import <DYSDK/DYNotificationTrack.h>
- Add the following method to your app’s AppDelegate class:-(void)application:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10")) { if([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) { [DYNotificationTrack trackClicked:userInfo]; } } }
- Add the following code in your app’s AppDelegate method application didFinishLaunchingWithOptions
if (launchOptions!=nil) { NSMutableDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (SYSTEM_VERSION_LESS_THAN(@"10")) { [DYNotificationTrack trackClicked:userInfo]; } }
- Add the following to your app’s AppDelegate header file to add two macros that determine the device’s iOS version and imports Dynamic Yield’s tracking class:
- Deep link handling
- Dynamic Yield launches any deep link URL within the push notification message content by calling [[UIApplication sharedApplication] openURL:url];
- The app should handle the deep link action (open the cart/show product / etc.) by implementing the delegate method:
application:openURL:options:
Configuring Push Notifications for Android
This procedure was verified on January 18th, 2018. Changes made by Google after this date may affect this procedure.
-
- Enable Push Notifications for your App in Firebase and get the Firebase project Server Key
- Open the Firebase Developers Console. If you don’t have a Firebase account, create one.
- If you don’t have a project, create a new one and follow instructions to add your app.
- Select the project that includes your app.
- On your Project’s Overview page, click the gear icon and then on Project Settings.
- On the Settings page, click on Cloud Messaging.
- In the Project Credentials section, hover over the Server Key and copy the key to your computer’s clipboard.
- Send the Server Key to Dynamic Yield.
- Configure push notifications in your app Android Studio project
Ensure that your app’s build.gradle file includes the following in the dependencies clause:dependencies { ... compile 'com.google.firebase:firebase-messaging:9.4.0' ... }
Note: The firebase-messaging version might be different
At the end of the file, add:
apply plugin: 'com.google.gms.google-services'
- Displaying your app icon in your app Android Studio project
In order to display your app icon in push notifications, you need to create an image file with the icon. Make sure your file is of the PNG file format with a transparent background.
Rename the image dy_notification_small_icon.png and add it to the drawable folder in your project.
- Allow Dynamic Yield to render and track delivery of push notifications once they arrive
- If all Push Notifications sent to your app are handled by Dynamic Yield, add the following service to the manifest file:
<service android:name="com.dynamicyield.dypush.DYFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>
- Otherwise, if your app receives additional push notifications not handled by Dynamic Yield:
Add the following call in method onMessageReceived of class FirebaseMessagingService:DYPushNotifHandler.getInstance().handlePushMessage( remoteMessage, ctx);
- If all Push Notifications sent to your app are handled by Dynamic Yield, add the following service to the manifest file:
- Allow Dynamic Yield to render notifications
Add the following to the manifest file:<receiver android:name="com.dynamicyield.dypush.dycarousel.DYCarouselReceiver"> <intent-filter> <action android:name="com.dynamicyield.dyapi.CAROUSALNOTIFICATIONFIRED" /> </intent-filter> </receiver>
- User push notifications registration handling.
The device token has to be passed to the Dynamic Yield SDK in order to register the user to Dynamic Yield push notifications- Make sure the Dynamic Yield SDK is initialized by calling the setContextAndSecretmethod in the DYApi class.
- Call the following code whenever the device token is received or updated:DYApi.getInstance().setPushNotificationId(String token);
- The device token can be obtained by calling FirebaseInstanceId.getInstance().getToken(), updates for the token can be obtained by implementing the Firebase class: FirebaseInstanceIdService
- In case your app includes logic enabling users to subscribe and unsubscribe from push notifications, use the following methods to notify Dynamic Yield:
- Subscribe: subscribeToPushNotifications( )
- Unsubscribe: unSubscribeFromPushNotifications( )
- Report push notification user clicks
Every Push Notification click has to be reported to Dynamic Yield to enable reporting. To do this, add the following code to the onCreate() method of the main activity as well as any activity reachable by clicking on a push notification deeplink:
Intent intent = getIntent(); Bundle bundle = intent.getExtras(); if( bundle != null ) { DYApi.trackNotificationClick(bundle, getApplicationContext()); }
- Deep link handling
Dynamic Yield enables configuring custom deeplinks called within push notifications click (the notification, button, image, etc.). To allow this, ensure that your app activities in the manifest are configured accordingly. For more information see: https://developer.android.com/training/app-links/deep-linking.html
- Enable Push Notifications for your App in Firebase and get the Firebase project Server Key