Giter VIP home page Giter VIP logo

salesforcemarketingcloudsdkxamarin's Introduction

SalesforceMarketingCloudSDKXamarin

marketingcloudsdk-8

How to init

Droid:

In MainActivity

internal static MainActivity Instance { get; private set; }
public override void OnCreate()
{
  base.OnCreate();


//Other code
Instance = this;
 var startMarketingSdk = new StartMCSdk();
 startMarketingSdk.StartSdk();
}

StartMCSdk class

using System;
using Android.Runtime;
using xxxxx.Droid.MessagingServices.MCListener;
using Com.Salesforce.Marketingcloud;
using Com.Salesforce.Marketingcloud.Notifications;
using Com.Salesforce.Marketingcloud.Sfmcsdk;

namespace xxxxx.Droid.MessagingServices
{
  [Preserve(AllMembers = true)]
  public class StartMCSdk : Java.Lang.Object, ISFMCSdkReadyListener, ILogListener
  {
      public void StartSdk()
      {
          try
          {
              // Initialize logging _before_ initializing the SDK to avoid losing valuable debugging information.
#if DEBUG
              SFMCSdk.SetLogging(LogLevel.Debug, this);
              MarketingCloudSdk.LogLevel = MCLogListener.Verbose;
              MarketingCloudSdk.SetLogListener(new MCWriteLogListener());
#endif

               var appID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
               var accessToken = "xxxxxxxxxxxxxxxxx";
               var appEndpoint = "https://mcxxxxxxxxxxx.device.marketingcloudapis.com/";
               var mid = "0000000000";
               var senderId = "0000000000";
               var inbox = true;
               var pushAnalytics = true;

              var marketingCloudConfig = MarketingCloudConfig
                     .InvokeBuilder()
                     .SetApplicationId(appID)
                     .SetAccessToken(accessToken)
                     .SetSenderId(senderId)
                     .SetMid(mid)
                     .SetGeofencingEnabled(true)
                     .SetDelayRegistrationUntilContactKeyIsSet(true)
                     .SetAnalyticsEnabled(pushAnalytics)
                     .SetInboxEnabled(inbox)
                     .SetMarketingCloudServerUrl(appEndpoint)
                     .SetPiAnalyticsEnabled(true)
                     .SetProximityEnabled(true)
                     .SetNotificationCustomizationOptions(NotificationCustomizationOptions.Create(Resource.Mipmap.app_icon))
                     .Build(MainApplication.Instance);

              // Init a SFMCSdk builder
              var config = new SFMCSdkModuleConfig.Builder();
              config.PushModuleConfig = marketingCloudConfig;
              var builder = config.Build();
              SFMCSdk.Configure(MainApplication.Instance, builder);
              SFMCSdk.RequestSdk(this);

              var sdkListner = new MCSdkListner(MainApplication.Instance);
              MarketingCloudSdk.Init(MainApplication.Instance, marketingCloudConfig, sdkListner);

          }
          catch (Exception ex)
          {
              System.Diagnostics.Debug.WriteLine(ex);
          }
      }

      public void Ready(SFMCSdk sdk)
      {
      }

      public void Out(LogLevel level, string tag, string message, Java.Lang.Throwable throwable)
      {
          LogInformation(tag, message);
      }

      void LogInformation(string methodName, object information) => Debug.WriteLine($"\nMethod: {methodName}\nInfo: {information}");
  }
}

Write Logs: new MCWriteLogListener()

using System;
using System.Diagnostics;
using Android.Runtime;
using Com.Salesforce.Marketingcloud;
using Java.Interop;
using Java.Lang;

namespace xxxxx.Droid.MessagingServices.MCListener
{
    [Preserve(AllMembers = true)]
    class MCWriteLogListener : Java.Lang.Object, IMCLogListener, IJavaObject, IDisposable, IJavaPeerable
    {
        public void Out(int level, string tag, string message, Throwable throwable)
        {
            LogInformation(tag, message);

        }
        void LogInformation(string methodName, object information) => Debug.WriteLine($"\nMethod: {methodName}\nInfo: {information}");

    }
}

new MCSdkListner()

using System;
using System.Threading.Tasks;
using Android.App;
using Android.Gms.Common;
using Android.Runtime;
using Com.Salesforce.Marketingcloud;

namespace xxxxx.Droid.MessagingServices
{
    [Preserve(AllMembers = true)]
    public class MCSdkListner : Java.Lang.Object, MarketingCloudSdk.IInitializationListener
    {
        private readonly Application _application;
        private readonly TaskCompletionSource<string> _taskCompletionSource;

        public MCSdkListner(Application application)
        {
            _application = application;
            _taskCompletionSource = new TaskCompletionSource<string>();
        }

        public void Complete(InitializationStatus status)
        {
            if (status.IsUsable)
            {
                var invokedStatus = status.InvokeStatus();

                if (invokedStatus == InitializationStatus.Status.CompletedWithDegradedFunctionality)
                {

                    if (status.LocationsError())
                    {
                        GoogleApiAvailability.Instance.ShowErrorNotification(_application, status.PlayServicesStatus());
                    }
                    else if (status.MessagingPermissionError())
                    {

                        System.Diagnostics.Debug.WriteLine("Location permission was denied.");
                    }
                }

                _taskCompletionSource.TrySetResult(default(string));
            }
            else
            {

                var error = status.UnrecoverableException();
                System.Diagnostics.Debug.WriteLine(error);
                _taskCompletionSource.TrySetException(new Exception(error.ToString()));
            }
        }

    }
}

new MCReadyListener();

using System;
using Android.Runtime;
using Com.Salesforce.Marketingcloud;

namespace xxxxx.Droid.MessagingServices.MCListener
{
    [Preserve(AllMembers = true)]
    public class MCReadyListener : Java.Lang.Object, MarketingCloudSdk.IWhenReadyListener
    {
        private string _contactKey { get; set; } = string.Empty;
        public MCReadyListener(string contactKey)
        {
            _contactKey = contactKey;
        }

        public void Ready(MarketingCloudSdk sdk)
        {
            System.Diagnostics.Debug.WriteLine(_contactKey);

            MarketingCloudSdk.Instance.PushMessageManager.EnablePush();
            var success = MarketingCloudSdk.Instance.RegistrationManager.Edit().SetContactKey(_contactKey).Commit();
            string contactkey = MarketingCloudSdk.Instance.RegistrationManager.ContactKey;
            var sdkstate1 = sdk.SdkState.ToString();
        }
    }
}

iOS

In AppDelegate

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{

//Other Code
 var startMarketingSDK = new StartMCSdk();
 startMarketingSDK.MarketingSdk();
}


[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    Messaging.SharedInstance.ApnsToken = deviceToken;
    SFMCSdk.Mp.SetDeviceToken(deviceToken);
}

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
    MobilePushSDK.SharedInstance().Sfmc_setNotificationRequest(response.Notification.Request);
    //SFMCSdk.Mp.NotificationRequest(response.Notification.Request);
    completionHandler?.Invoke();
}


// iOS >=10, Fires when application recieves a notification while in the foreground
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public static void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
   MobilePushSDK.SharedInstance().Sfmc_setNotificationRequest(notification.Request);
}

// iOS <=9, Fires when application recieves a notification while in the foreground
[Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    var request = UNNotificationRequest.FromIdentifier(new NSUuid().ToString(), new UNMutableNotificationContent { UserInfo = userInfo }, null);
    MobilePushSDK.SharedInstance().Sfmc_setNotificationUserInfo(userInfo);
    completionHandler.Invoke(UIBackgroundFetchResult.NewData);
}



using Foundation;
using System;
using System.Diagnostics;
using UIKit;
using UserNotifications;
using SFMCiOS;

namespace xxxxx.iOS.MessagingServices
{
    [Preserve(AllMembers = true)]
    public class StartMCSdk
    {
        public void MarketingSdk()
        {
            try
            {
                var appID = "xxxxxx-xxxx-xxx-xx-xxxxxx";
                var accessToken = "xxxxxxx";
                var appEndpoint = "https://mcxxxxxxxx.device.marketingcloudapis.com/";
                var mid = "xxxxxx";
        
                // Define features of MobilePush your app will use.
                var inbox = true;
                var location = false;
                var pushAnalytics = true;

#if DEBUG
                SFMCSdk.SetLoggerWithLogLevel(SFMCSdkLogLevel.Debug, new SFMCSdkLogger());
#endif

                // Use the Mobile Push Config Builder to configure the Mobile Push Module. This gives you the maximum flexibility in SDK configuration.
                // The builder lets you configure the module parameters at runtime.
                var mobilePushConfiguration = new PushConfigBuilder(appID)
                    .SetAccessToken(accessToken)
                    .SetMarketingCloudServerUrl(appEndpoint)
                    .SetMid(mid)
                    .SetInboxEnabled(inbox)
                    .SetLocationEnabled(location)
                    .SetAnalyticsEnabled(pushAnalytics)
                    .Build;


                // Set the completion handler to take action when module initialization is completed. The result indicates if initialization was sucesfull or not.
                // Seting the completion handler is optional.
                Action<SFMCSdkOperationResult> completionHandler = (result) =>
                {
                    if (result == SFMCSdkOperationResult.Success)
                    {
                        LogInformation("CompletionHandler", result);

                        SFMCSdk.Mp.PushEnabled = true;
                        SFMCSdk.Mp.SetURLHandlingDelegate(new URLHandlingDelegate());

                    }
                    else if (result == SFMCSdkOperationResult.Error)
                    {
                        LogInformation("CompletionHandler", result);
                    }
                    else if (result == SFMCSdkOperationResult.Cancelled)
                    {
                        LogInformation("CompletionHandler", result);
                    }
                    else if (result == SFMCSdkOperationResult.Timeout)
                    {
                        LogInformation("CompletionHandler", result);
                    }
                };

                //// Once you've created the mobile push configuration, intialize the SDK.
                SFMCSdk.InitializeSdk(new SFMCSdkConfigBuilder().SetPushWithConfig(mobilePushConfiguration, completionHandler).Build);

                if (UIApplication.SharedApplication.BackgroundRefreshStatus == UIBackgroundRefreshStatus.Available)
                {
                    Debug.WriteLine("Enabling background refresh");
                    UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        void LogInformation(string methodName, object information) => Debug.WriteLine($"\nResult: {methodName}\nInfo: {information}");
    }
}

new SFMCSdkLogger();

using System;
using System.Diagnostics;
using SFMCiOS;

namespace xxxxx.iOS.MessagingServices
{
    public class SFMCSdkLogger : SFMCSdkLogOutputter
    {
        public override void OutWithLevel(SFMCSdkLogLevel level, string subsystem, SFMCSdkLoggerCategory category, string message)
        {
            LogInformation(subsystem, message);
        }

        void LogInformation(string methodName, object information) => Debug.WriteLine($"\nSubsystem: {methodName}\nInfo: {information}");
    }
}

new URLHandlingDelegate();

using System;
using System.Diagnostics;
using Foundation;
using SFMCiOS;
using UIKit;

namespace xxxxx.iOS.MessagingServices
{
    public class URLHandlingDelegate : SFMCSdkURLHandlingDelegate
    {
        public override void Type(NSUrl url, string type)
        {
            Debug.WriteLine(string.Format("HandleURL: {0} {1}", type, url));

            if (UIApplication.SharedApplication.CanOpenUrl(url))
            {
                Debug.WriteLine(string.Format("Can open: " + url));
                var options = new NSDictionary();
                UIApplication.SharedApplication.OpenUrl(url, options, (success) => Debug.WriteLine("OpenURL: " + success));
            }
            else
            {
                Debug.WriteLine("Cannot open URL: " + url);
            }
        }

    }
}

salesforcemarketingcloudsdkxamarin's People

Contributors

taku-ka avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

salesforcemarketingcloudsdkxamarin's Issues

Compatibility with Android 12

Hello,
In a project I'm working on we need to update the marketing cloud sdk to an 8.x version, that provides the mandatory compatibility for Android 12.

I tried to substitute the marketing cloud aar with the version 8.0.6 in the binding library but I'm facing some parsing issues that prevents the binding library to build.

There is any chance that you already solved this issue?

Thank you in advance

Paid Collab Opportunity.

Hi @taku-ka,

This is the only SDK that we can find for Xamarin implementation for Marketing Cloud and I would say this is the best shot that our company have to land a project with a client.

We are trying to implement this SDK for IOS and Android but we need guidance and support for this SDK. We would like to invite you for a paid collab with us for this project.

Please contact me by e-mail ([email protected]) if you are interested.

iOS connection Not-opted-in

Hi @taku-ka, thans in advance for your code.
We've implemented your solution in Xamarin for iOS but we are facing the problem that in SF the connection is not opening as we expect: they receive the status not-opted-in and they can't communicate with our APP.
We are thinking about a paid collab with you because we really need this solution and we would like to implement something like your solution. Please contact me by e-mail ([email protected]) if you are interested or if you have some ideas.
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.