Handling Qonversion Notifications

iOS Push Notifications

1. Set notifications token

Override application(_:didRegisterForRemoteNotificationsWithDeviceToken:) to handle notifications token.
Call the Qonversion.setNotificationsToken() method to enable Qonversion to send the push notifications.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Qonversion.setNotificationsToken(deviceToken)
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [Qonversion setNotificationsToken:deviceToken];
}

2. Handle notifications

Override the userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response method to handle received notifications. Call Qonversion.handleNotification() with the notification data. The method returns true when a push notification was received from Qonversion and Qonversion SDK will try to handle it. If you have not set up the in-app screen with Qonversion, you need to handle a notification yourself.
For push notifications from other services, the method returns false, so you also need to handle a notification yourself.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let isPushHandled: Bool = Qonversion.handleNotification(response.notification.request.content.userInfo)
    if !isPushHandled {
      // Handle notification yourself
    }
    completionHandler()
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
         withCompletionHandler:(void (^)(void))completionHandler {
    BOOL isPushHandled = [Qonversion handleNotification:response.notification.request.content.userInfo];
    if (!isPushHandled) {
      // Handle notification yourself
    }
    completionHandler();
}

Android Push Notifications

1. Set notifications token

To enable Qonversion to send push notifications сall Qonversion.setNotificationsToken() and pass a device registration token. Here you can find more information about the ways to retrieve the token.
You have to call the setNotifications method for every newly generated token.

Qonversion.setNotificationsToken(token)
Qonversion.setNotificationsToken(token);

2. Handle notifications

To define what happens when users click on push notifications you have to specify an instance of the PendingIntent object and pass the RemoteMessage as extra data. Then call the setContentIntent() method of the NotificationCompat.Builder.

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)
    
    val title = remoteMessage.data["title"]
    val body = remoteMessage.data["body"]

    // Create an explicit intent for an Activity in your app
    val intent = Intent(this, NotificationActivity::class.java)
    // If you need, set the intent flag for the activity
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    // Save the RemoteMessage object as extra data
    intent.putExtra(INTENT_REMOTE_MESSAGE, remoteMessage)
    // Flag FLAG_ONE_SHOT indicates that this PendingIntent can be used only once
    val pendingIntent: PendingIntent =
        PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)

    val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle(title)
        .setContentText(body)
        .setSmallIcon(R.drawable.ic_notification)
        // Set the intent that will fire when the user taps the notification
        .setContentIntent(pendingIntent)

    with(NotificationManagerCompat.from(this)) {
        // For notificationId use any unique value you want to be an ID for the notification
        notify(notificationId, notificationBuilder.build())
    }
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String title = remoteMessage.getData().get("title");
    String body = remoteMessage.getData().get("body");

    // Create an explicit intent for an Activity in your app
    Intent intent = new Intent(this, NotificationActivity.class);
    // If you need, set the intent flag for activity
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // Save the RemoteMessage object as extra data
    intent.putExtra(INTENT_REMOTE_MESSAGE, remoteMessage);
    // Flag FLAG_ONE_SHOT indicates that this PendingIntent can be used only once
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_notification)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent);
  
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    // For notificationId use any unique value you want to be an ID for the notification. 
    notificationManager.notify(notificationId, notificationBuilder.build());
}

🚧

Applications that target Android 12 or higher can't start activities from services or broadcast receivers that are used as notification trampolines. Make sure that you are using Activity as an automation push handler, used for notification intent.

In the NotificationActivity onCreate() method retrieve an instance of the RemoteMessage and call Qonversion.handleNotification() with the message data. The method returns true when a push notification was received from Qonversion and Qonversion SDK will try to handle it. If you have not set up the in-app screen with Qonversion, you need to handle a notification yourself.
For push notifications from other services, the method returns false, so you also need to handle a notification yourself.

val remoteMessage: RemoteMessage? = intent.getParcelableExtra(INTENT_REMOTE_MESSAGE)
if (remoteMessage != null && !Qonversion.handleNotification(remoteMessage.data)) {
   // Handle notification yourself
}
RemoteMessage remoteMessage = getIntent().getParcelableExtra(INTENT_REMOTE_MESSAGE);
if (remoteMessage != null && !Qonversion.handleNotification(remoteMessage.getData)) {
    // Handle notification yourself
}

Cross-platform Push Notifications

1. Set notifications token

Call the Qonversion.setNotificationsToken() method to enable Qonversion to send the push notifications. Pass APNs token for iOS devices or Firebase Cloud Messaging token for Android devices.

Qonversion.setNotificationsToken(notificationsToken);
Qonversion.setNotificationsToken(notificationsToken);
Qonversion.SetNotificationsToken(notificationsToken);

2. Handle notifications

Сall Qonversion.handleNotification() with the notification data to define what happens when users click on push notifications. The method returns true when a push notification was received from Qonversion and Qonversion SDK will try to handle it. If you have not set up the in-app screen with Qonversion, you need to handle a notification yourself.
For push notifications from other services, the method returns false, so you also need to handle a notification yourself.

📘

Notification data

The handleNotification method accepts an object instance of Map. Retrieving this object depends on your implementation of push notifications.

final isNotificationHandled = await Qonversion.handleNotification(notificationData);
if (!isNotificationHandled) {
  // Handle notification yourself
}
const isNotificationHandled = await Qonversion.handleNotification(notificationData);
if (!isNotificationHandled) {
  // Handle notification yourself
}
bool isNotificationHandled = Qonversion.HandleNotification(notificationData);
if (!isNotificationHandled)
{
  // Handle notification yourself
}

Custom payload

You have an option to add a custom payload to the notification while creating it in the Qonversion dashboard. The data should be a valid JSON object. Neither JSON arrays nor simple values like strings or integers are allowed. It is done to simplify the method's return value type. Then you can get parsed payload on your client side using the following method of the corresponding SDK providing the received push notification data.

let customPayload: [AnyHashable:Any]? = Qonversion.getNotificationCustomPayload(response.notification.request.content.userInfo)
NSDictionary *customPayload = [Qonversion getNotificationCustomPayload:response.notification.request.content.userInfo];
val customPayload: Map<String, Any?>? = Qonversion.getNotificationCustomPayload(remoteMessage.data)
final Map<String, Object> customPayload = Qonversion.getNotificationCustomPayload(remoteMessage.getData());
final customPayload = await Qonversion.getNotificationCustomPayload(notificationData);
const customPayload = await Qonversion.getNotificationCustomPayload(notificationData);
Dictionary<string, object> customPayload = Qonversion.GetNotificationCustomPayload(notificationData);

The method return value is nullable for the cases when there is no custom payload in the notification, or any error occurs while parsing.