How can I open an App using it own background service

268 Views Asked by At

I'm creating Xamarin.Forms App and it has a service that runs in the background monitoring if any messages arrive. If a message arrives, the service should open the App. I tried to open the App using Intent (Dependency Service) and I also tried using Xamarin.Essentials. Neither way worked. My last attempt was with Xamarin.Essentials and gave the following error: [Error][1] [1]: https://i.stack.imgur.com/5jPGf.png

This is my code using Intent (Dependency Service):

//Intent (Dependency Service)
   string teste = "com.saferit.saferproject";
   openAppManager = DependencyService.Get<IAppHandler>();
   openAppManager.Initialize(teste);
   openAppManager.LaunchApp();

[enter image description here][2] [2]: https://i.stack.imgur.com/lLx3g.png

This is my native code for Android

public void LaunchApp()
    {
        Intent intent = new Intent();
        intent.SetClassName(Package, "activity");
        intent.AddFlags(ActivityFlags.NewTask);
        Xamarin.Forms.Forms.Context.StartActivity(intent);
    }

[Complete Native Code][3] [3]: https://i.stack.imgur.com/G4zOV.png

This is my AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.saferit.saferproject" android:installLocation="preferExternal">
    <uses-sdk android:minSdkVersion="27" android:targetSdkVersion="29" />
    <!--<application android:label="SaferProject.Android"></application>-->
  <application android:label="com.saferit.saferproject" android:icon="@drawable/logo">
    <activity android:icon="@drawable/logo" android:label="com.saferit.saferproject" android:name="com.saferit.saferproject">
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="com.saferit.saferproject" />
      </intent-filter>
    </activity>
  </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

This is my attempt using Xamarin.Essential:

  //Xamarin.Essentials
  var appname = "com.saferit.saferproject://";
  var supportsUri = await Launcher.CanOpenAsync(appname);
  if (supportsUri)
     await Launcher.OpenAsync(appname);

Looking for here at stackoverflow I found this question: Open Notification list page when user clicks on navigation

I tried to implement it on my App but I also couldn't get it to work. In all three cases the errors are very similar. I think the problem is in the configuration of the URI of my App. Can someone help me?

0

There are 0 best solutions below