Skip to content

Push Notifications

With SDK

You can track when a user opens a MovableInk Push Notification by calling handlePushNotificationOpened with the notification's payload.

import * as React from 'react';
import { Notifications, type Registered } from 'react-native-notifications';
import RNMovableInk from '@movable/react-native-sdk';

export default function App() {
  React.useEffect(() => {
    // Register for remote notifications
    Notifications.registerRemoteNotifications();

    // Handle device token updates
    const registerForNotificationsSubscription =
      Notifications.events().registerRemoteNotificationsRegistered(
        (event: Registered) => {
          console.log(event.deviceToken);
        }
      );

    // Handle notification taps
    const openedSubscription =
      Notifications.events().registerNotificationOpened(
        (notification, completion) => {
          RNMovableInk.handlePushNotificationOpened(notification.payload);

          completion();
        }
      );

    return () => {
      registerForNotificationsSubscription.remove();
      openedSubscription?.remove();
    };
  }, []);

Without SDK

If you're not using the SDK, you can still track this event by manually making a request to the URL in the payload.

const openedSubscription =
  Notifications.events().registerNotificationOpened(
    async (notification, completion) => {
      let url;

      if (notification.payload.mi?.url) {
        url = notification.payload.mi.url;
      }
      else if (notification.payload.data?.mi_url) {
        url = notification.payload.data?.mi_url;
      }

      if (url) {
        try {
          await fetch(url);
        } catch (error) {
          // Handle error
        }
      }

      completion();
    }
  );

MovableInk Notification payload examples:

iOS

{
  "aps": {
    "alert": {
      "title": "Special Offer Just for You!",
      "body": "Open to unlock exclusive savings."
    },
    "sound": "default"
  },
  "mi": {
    "url": "https://mi.example.com/p/push/abcd12345"
  }
}
{
  "aps": {
    "alert": {
      "title": "Special Offer Just for You!",
      "body": "Open to unlock exclusive savings."
    },
    "sound": "default"
  },
  "data": {
    "mi_url": "https://mi.example.com/deeplink/abc123",
    "mi_source": "davinci"
  }
}

Android

{
  "notification": {
    "title": "Special Offer Just for You!",
    "body": "Open to unlock exclusive savings."
  },
  "data": {
    "mi_url": "https://mi.example.com/deeplink/abc123",
    "mi_source": "davinci"
  }
}