sefidgaran / salesforce-marketing-cloud

Salesforce Marketing Cloud MobilePush Flutter SDK
https://pub.dev/packages/sfmc_plugin
MIT License
6 stars 21 forks source link

How to navigate to a specific page when I press the button on an In-App Message? #20

Open ItsNotPat opened 2 days ago

ItsNotPat commented 2 days ago

I created the In-App Message through Content builder. I added a button to the message and set the behavior to Open in app page. I set the URL to the web version of the Page I want to go to.

Upon testing, I received the In App Message. But when I press the button it directs to the browser.

sefidgaran commented 2 days ago

You may need to check if your app is already handeling universal deep linking.

ItsNotPat commented 2 days ago

Yes we do have a handler for deep linking already here's the code

Anrdoid Manifest

<intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- If a user clicks on a shared link that uses the "http" scheme, your
                     app should be able to delegate that traffic to "https". -->
                <data android:scheme="https" android:host="my.url" />

            </intent-filter>

Splash Page ( Initialize is fired on Init State )

Future<void> initializeSFMC() async {
    /// iOS only
    SfmcPlugin().setHandler(handler);

    if (Platform.isIOS) {
      await SfmcPlugin().initialize(
        appId: config.toNonNullableString('SFMC_APP_ID'),
        accessToken: config.toNonNullableString('SFMC_ACCESS_TOKEN'),
        mid: config.toNonNullableString('SFMC_MID'),
        sfmcURL: config['SFMC_URL'] != '' ? config['SFMC_URL'] : null,
        senderId: config.toNonNullableString('SFMC_SENDER_ID'),

        /// Set delayRegistration on iOS only,
        /// delayRegistration on Android is by default true
        delayRegistration: true,

        /// Set analytics on iOS only,
        /// analytics on Android is by default true
        analytics: true,
      );
    }
  }

Future handler(MethodCall methodCall) async {
  switch (methodCall.method) {
    case 'handle_url':
      String? url = methodCall.arguments['url'];
      if (url != null) {
        String webUrl = config['WEB_URL'] ?? '';
        final notifUrl = url.replaceAll(webUrl, '');
        final splitUrl = notifUrl.split('?');
        if (splitUrl.isNotEmpty) {
          urlFromNotif.value = splitUrl.first;
          if (notifUrl.contains('utm_') && splitUrl.length > 1) {
            logNotifCampaignDetails(notifUrl);
          }
        }
      }
  }
}

Home Page (Init Deep links is fired on Init State )

late AppLinks _appLinks;

  Future<void> initDeepLinks() async {
    _appLinks = AppLinks();

    final appLink = await _appLinks.getInitialLink();
    if (appLink != null) {
      openAppLink(appLink);
    }

    _appLinks.uriLinkStream.listen((uri) {
      openAppLink(uri);
    });
  }

  Future handler(MethodCall methodCall) async {
    switch (methodCall.method) {
      case 'handle_url':
        String? url = methodCall.arguments['url'];
        if (url != null) {
          toUrlFromNotif(url);
        }
    }
  }

  void openAppLink(Uri uri) {
    toUrlFromNotif(uri.toString());
    //code if wants to push to routing
    // _navigatorKey.currentState?.pushNamed(uri.fragment);
  }

  Future<void> toUrlFromNotif(String url) async {
    String webUrl = config['WEB_URL'] ?? '';
    final notifUrl = url.replaceAll(webUrl, '');
    final splitUrl = notifUrl.split('?');
    if (splitUrl.isNotEmpty) {
      urlFromNotif.value = splitUrl.first;

      if (notifUrl.contains('utm_') && splitUrl.length > 1) {
        logNotifCampaignDetails(notifUrl);
      }
    }
  }