1

In my react-native app, I am using clevertap for sending push notifications. I'm receiving notifications on mobile but not able to read its payload.

Dheeraj Bari
  • 51
  • 1
  • 3

2 Answers2

3

To handle the iOS push notifications payload and process the user's response to a notification, iOS provides delegate method

userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

You can implement the same by following steps:

  • Navigate to your AppDelegate file > And the framework

    #import <UserNotifications/UserNotifications.h>

  • Also, add UNUserNotificationCenterDelegate

    @interface AppDelegate () <UIApplicationDelegate,UNUserNotificationCenterDelegate>

  • Handling delegate methods for UserNotifications

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

   NSLog(@"User Info : %@",response.notification.request.content.userInfo); //for getting response payload data
   completionHandler();
}

Additionally, if you wish to determine whether a notification originated from CleverTap, call this method:

- (BOOL)isCleverTapNotification:(NSDictionary *)payload;

You must manually call the SDK as follows/ Add following CleverTap code to your AppDelegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {

    if ([[CleverTap sharedInstance] isCleverTapNotification:response.notification.request.content.userInfo]) {
       ...
    }
    completionHandler();
}

Hope this helps. For further questions, you can post on https://community.clevertap.com/

2

You can render Push notification using CleverTap in two different ways.

Way 1: Allowing CleverTap to handle Push Notifications directly. For this just mention the following code in your Manifest file

<service
    android:name="com.clevertap.android.sdk.FcmTokenListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

<service
    android:name="com.clevertap.android.sdk.FcmMessageListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

Way 2: Handle Payload at your end. You can do this as follow.

Step 1: Add your Custom FCM Message Listener in your Manifest file.

<service
android:name="com.your.package.MyFcmMessageListenerService">
<intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>

Step 2: In Your FCM Message Listener Service, You will get the Payload, which you can handle it in the following way.

public class MyFcmMessageListenerService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage message){
    try {
        if (message.getData().size() > 0) {
            Bundle extras = new Bundle();
            for (Map.Entry<String, String> entry : message.getData().entrySet()) {
                extras.putString(entry.getKey(), entry.getValue());
            }

            NotificationInfo info = CleverTapAPI.getNotificationInfo(extras); //info will get the payload.

            if (info.fromCleverTap) {
                CleverTapAPI.createNotification(getApplicationContext(), extras);
            } else {
                // not from CleverTap handle yourself or pass to another provider
            }
        }
    } catch (Throwable t) {
       Log.d("MYFCMLIST", "Error parsing FCM message", t);
    }
}

}

Hope this helps. For further questions, you can write your query to support@clevertap.com

parth dani
  • 21
  • 1