Customer Engagement

Adding Flutter dependency

Step 1: Ensure to complete basic setup for Smartech Base Flutter package
Go to Basic Setup to start integrating your Android project with Smartech Base Flutter package.

Step 2: Complete setup for Smartech Push Flutter package
Implement Smartech Push Flutter package in pubspecs.yaml file under dependencies:

By using pub

smartech_push: ^3.2.1

For Android SDK

Step 1: Integrate Firebase Cloud Messaging in your app
Please follow Google’s Documentation to set up native FCM integration in your app, if it is not already done.

Provide token via setDevicePushToken() and notification payload via handlePushNotification() methods to CEE SDK. handlePushNotification() method will only display a notification if the Notification payload originated from Netcore and will safely ignore if not. The code of the class should look similar to the following:

public class <YourFirebaseMessagingServiceClass> extends FirebaseMessagingService {

    @Override
    public void onNewToken(@NonNull String token) {
        super.onNewToken(token);
        SmartPush.getInstance(new WeakReference<Context>(this)).setDevicePushToken(token);
      
        //<Your code>
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        boolean pushHandledBySmartech = SmartPush.getInstance(new WeakReference<>(getApplicationContext())).handlePushNotification(remoteMessage.getData().toString())
        if (!pushHandledBySmartech){
       
         // Handle notification from other sources.
            
       }
    }
}
class MyFirebaseMessagingService : FirebaseMessagingService() {

 override fun onNewToken(token: String) {
 super.onNewToken(token)
 SmartPush.getInstance(WeakReference(this)).setDevicePushToken(token)
 }

 override fun onMessageReceived(message: RemoteMessage) {
 super.onMessageReceived(message)
 SmartPush.getInstance(WeakReference(this)).handlePushNotification(message.data.toString())
 }
}

Retrieving Deeplink Data

Register the BroadcastReceiver inside the Application class.

DeeplinkReceivers deeplinkReceiver = new DeeplinkReceivers();
IntentFilter filter = new IntentFilter("com.smartech.EVENT_PN_INBOX_CLICK");
registerReceiver(deeplinkReceiver, filter);
val deeplinkReceiver = DeeplinkReceivers()
val filter = IntentFilter("com.smartech.EVENT_PN_INBOX_CLICK")
registerReceiver(deeplinkReceiver, filter)

Write the following code in the onCreate function of the launcher activity of your application.

new DeeplinkReceivers().onReceive(this, getIntent());
DeeplinkReceivers().onReceive(this, intent)

For iOS SDK

Setting up Push Notifications for your iOS project is a quick 8 steps process

Ensure you have done the Basic Setup before you start integrating push notifications.

Step 1: Adding SmartPush SDK dependency - CocoaPods

  1. In the Podfile add the SmartPush-iOS-SDK as a dependency.
pod 'SmartPush-iOS-SDK', '~> 1.0.0'
  1. Run the following command to update the added pod in your Podfile.
pod install

Once your installation is complete, you will have to open the workspace file (YOUR-PROJECT-NAME.xcworkspace) that is created.

  1. Import SmartPush SDK in AppDelegate class.
#import <SmartPush/SmartPush.h>
import SmartPush

Step 2: Adding & Configuring of Extensions

Click here to add & configure extensions.

Step 3: Register For Remote Notifications

In order to receive push notifications you need to send the device token you received in method didRegisterForRemoteNotificationsWithDeviceToken to CEE . Refer the following code:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [[SmartPush sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  SmartPush.sharedInstance().didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
}

For an error/failure for registering of remote notification.

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  [[SmartPush sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  SmartPush.sharedInstance().didFailToRegisterForRemoteNotificationsWithError(error)
}

Step 4: Implementation of UNUserNotificationCenterDelegate methods

Implement the following code in delegate methods of UNUserNotificationCenter. First import following frameworks provided by apple for UNUserNotificationCenterDelegate

#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
import UserNotifications
import UserNotificationsUI

Confirm UNUserNotificationCenterDelegate in AppDelegate class

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //....
  
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  
  //...
  return YES;
}

//Confirming UNUserNotificationCenterDelegate protocol

@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  
  UNUserNotificationCenter.current().delegate = self
  
  //...
  return true
}

//Confirming UNUserNotificationCenterDelegate protocol

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

}

Implement UNUserNotificationCenterDelegate methods in AppDelegate class

#pragma mark - UNUserNotificationCenterDelegate Methods
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  [[SmartPush sharedInstance] willPresentForegroundNotification:notification];
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  [[SmartPush sharedInstance] didReceiveNotificationResponse:response];
  completionHandler();
}
//MARK:- UNUserNotificationCenterDelegate Methods
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  SmartPush.sharedInstance().willPresentForegroundNotification(notification)
  completionHandler([.alert, .badge, .sound])
}
    
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  SmartPush.sharedInstance().didReceive(response)
  completionHandler()
}

Step 5: Set Notification Options

CEE SDK has a default method that sets the notification options with options Sound, Alert, Badge. For that, you need to call the method registerForPushNotificationWithDefaultAuthorizationOptions in AppDelegate class.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //...
  [[SmartPush sharedInstance] registerForPushNotificationWithDefaultAuthorizationOptions];
  //...
  return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
   SmartPush.sharedInstance().registerForPushNotificationWithDefaultAuthorizationOptions()
  //...
  return true
}

If you want to change options you can use the following method named setNotificationOptions to set the notification options in AppDelegate class in didFinishLaunchingWithOptions method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //...
  
  [[SmartPush sharedInstance] registerForPushNotificationWithAuthorizationOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound)];
  //...
  return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  SmartPush.sharedInstance().registerForPushNotification(authorizationOptions: [.alert, .badge, .sound])
  //...
  return true
}

Step 6: Configuring Capabilities of App

Enable the below app capabilities in the targets of your main app. (To know how to add capabilities check this developer document.)

  1. App Groups - Enable/Add and select a valid App Group for your app. This is used by App, Service Extension, and Content Extension to share data with each other. Make sure that you select the same App Group for App, Service Extension, and Content Extension.
  2. Background Modes - Enable/Add Background Modes and select Remote notifications. This allows the app to receive remote notifications when it is in the background.
  3. Push Notifications - Enable/Add Push Notifications.

Step 7: Enable Push Notification for App Id

Configure App Push Notifications feature for your App ID in Apple Developer Account. Check this document for details.

Step 8: APNs key setup

Click here for steps to create APNs key.

📘

Testing Push Notifications

  1. Add App Transport Security Settings with Allow Arbitrary Loads as YES in Info.plist of App, Service Extension, and Content Extension to support Http calls.
  2. Push Notifications can only be tested over a real device and NOT through XCode. You can use any iOS device (iPhone, iPad, iPod Touch) to test the push notifications.

Handling deeplink on Flutter

The below method will provide the deep link value and custom payload.

Smartech().onHandleDeeplinkAction((String? link, Map<dynamic, dynamic>? map, bool? isAfterTerminated) async {
      // Handle the deeplink action
 });

We also need to handle deeplink in the terminate state so add the below code inside the main.dart file or put inside the class called by your application.

Smartech().onHandleDeeplinkActionBackground(); //method required for Android platform only

Did this page help you?