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 'dart:async';
import 'package:movable_ink_plugin/currency.dart';
import 'package:movable_ink_plugin/movable_ink_plugin.dart';
import 'package:push/push.dart';

...

class _MyAppState extends State<MyApp> {
  ...

  VoidCallback? unsubscribeOnNewToken;
  VoidCallback? unsubscribeOnNotificationTap;

  @override
  void initState() {
    super.initState();

    initMovableInkListener();
    initPush();
  }

  @override
  void dispose() {
    super.dispose();

    ...

    unsubscribeOnNewToken?.call();
    unsubscribeOnNotificationTap?.call();
  }

  void initMovableInkListener() async {
    ...
  }

  void initPush() async {
    await Push.instance.requestPermission();
    Push.instance.registerForRemoteNotifications();

    unsubscribeOnNewToken = Push.instance.addOnNewToken((token) {
      print("push token: $token");
    });

    unsubscribeOnNotificationTap = Push.instance.addOnNotificationTap((data) {
      _movableInkPlugin.handlePushNotificationOpenedWithContent(data);
    });

    Push.instance.notificationTapWhichLaunchedAppFromTerminated.then((data) {
      if (data != null) {
        _movableInkPlugin.handlePushNotificationOpenedWithContent(data);
      }
    });
  }
}

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.

import 'package:http/http.dart' as http;

void handleMIPush(Map<String?, Object?> data) async {
  String? url;

  // nested mi object
  if (data['mi'] is Map) {
    final mi = data['mi'] as Map;

    if (mi['url'] is String) {
      url = mi['url'] as String;
    }
  }
  // flattened mi_url key
  else if (data['mi_url'] is String) {
    url = data['mi_url'] as String;
  }

  if (url != null && url.isNotEmpty) {
    await http.get(Uri.parse(url));
  }
}

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"
  }
}