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());
}

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
}

Crossplatform Push Notifications

📘

Flutter only

Currently available only on Flutter

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);

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<String, dynamic>. Retrieving this object depends on your implementation of push notifications in Flutter.

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

Did this page help you?