Inter App Communication – How to communicate between two Windows Universal Apps ?

Inter App Communication – How to communicate between two Windows Universal Apps ?

Communicating between Apps is one of the important factor that developers needs to take care of. This could be done via some services , background process like what we have seen in our previous post How to create App Service communication in Windows Universal Apps.  In this post we will see how to communicate from one Windows Universal App to another Windows Universal App. This is widely useful when the app has to do some  functionality such as  third party processing. In that kind of scenario the app can directly invoke the other app and perform  the required operation and get back to the same point from where it was left.

Consider an example when the app has a purchase option then the app can let the user to do the purchase by using the apps which he can trust the most or by directly invoking the banking apps he can complete his transaction. App2App

The following steps explains the communication from one app with the other.

Step 1: Add the Extensions to the app that can take the request from the other app. Set the Category to windows.protocol . and also set the Protocol name as paymentoption in the Package.appxmanifest file of the target application.

<Extensions>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="paymentoption">
<uap:Logo>Assets\Logo.png</uap:Logo>
<uap:Protocol>
</uap:Extension>
<Extensions>

Step 2: Now add the following code in the app, the Launcher class lets the app to call another app. The LaunchUriForResultsAsync method takes the protocol, which is accepted by  target app, in our sample we used  paymentoption as the protocol.

var option = new LauncherOptions();
option.TargetApplicationPackageFamilyName = "2fbfc802-fe97-4906-9136-e9ad58a63c70_ccm3mam8je5tm";
var result = await Launcher.LaunchUriForResultsAsync(new Uri("paymentoption:"), option);
if (result.Status == LaunchUriStatus.Success)
    {
       var response = result.Result;
       var isSuccess = Convert.ToBoolean(response["Success"]);
         if (isSuccess)
         {
           // success response
         }
    }

The LauncherOptions sets the TargetApplicationPackageFamilyName so that a particular app gets opened when these are more than one app which accepts the same Protocol.

Windows.ApplicationModel.Package.Current.Id.FamilyName

The above mentioned API gets the Family Name of the app.

Must Read : Developing Windows Universal App in Windows 10

Step 3: In the target app when the app gets Activated then on the OnActivated method check for the activation Kind and navigate the app to the respective page.

 

 protected override void OnActivated(IActivatedEventArgs args)
 {
   var protocolArgs = args as ProtocolForResultsActivatedEventArgs;
   var rootFrame = new Frame();
   if (args != null)
   {
     rootFrame.Navigate(typeof(MainPage), args);
   }
   else
   {
     rootFrame.Navigate(typeof(MainPage), null);
   }

   Window.Current.Content = rootFrame;
   Window.Current.Activate();
 }

Step 4: In the target app when the page navigation happens the ProtocolForResultsActivatedEventArgs object should be saved so that the response data can be sent back to the caller app.

ProtocolForResultsOperation response;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
  base.OnNavigatedTo(e);
  var option = e.Parameter as ProtocolForResultsActivatedEventArgs;
   if (option != null)
   {
     response = option.ProtocolForResultsOperation;
   }
}

Step 5: once the payment is done the Target app should return the data to the caller app.

The ReportCompleted method takes a ValueSet which lets the other app to know that the result of this transaction.

var result = new ValueSet();
result["Success"] = true;
  if (response != null)
  {
   response.ReportCompleted(result);
  }

Thus the App communicated with the target app and waits for its response, once the response is received from the target app then the current app proceeds.

I hope this post might have helped in creating apps that communicate between apps.

Arun Kumar

Arun kumar Surya Prakash, is a  Developer  Consultant. He  is a Microsoft Certified Solution  Developer  for Store App Development, and a Azure Solution Developer. His core skills is on developing Windows Store App and Cross Platform App development. You can follow him @arunnov04