Flutter: starting application from service for Android

0

I'm trying to start my application when I receive message from Firebase Messaging Service. I cloned source code of the firebase_messaging plugin and I'm modifying Android code of this plugin. To start my application I use intent.

@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
    // If application is running in the foreground use local broadcast to handle message.
    // Otherwise use the background isolate to handle message.
    startTextIntent();
    if (isApplicationForeground(this)) {
      Intent intent = new Intent(ACTION_REMOTE_MESSAGE);
      intent.putExtra(EXTRA_REMOTE_MESSAGE, remoteMessage);
      LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    } else {
      // If background isolate is not running yet, put message in queue and it will be handled
      // when the isolate starts.
      handleConference(remoteMessage);
      if (!isIsolateRunning.get()) {
        backgroundMessageQueue.add(remoteMessage);
      } else {
        final CountDownLatch latch = new CountDownLatch(1);
        new Handler(getMainLooper())
                .post(
                        new Runnable() {
                          @Override
                          public void run() {
                            executeDartCallbackInBackgroundIsolate(
                                    FlutterFirebaseMessagingService.this, remoteMessage, latch);
                          }
                        });
        try {
          latch.await();
        } catch (InterruptedException ex) {
          Log.i(TAG, "Exception waiting to execute Dart callback", ex);
        }
      }
    }
  }

  void startTextIntent() {
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    sharingIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    sharingIntent.putExtra(Intent.EXTRA_TEXT, "text from firebase service");
    backgroundContext.startActivity(Intent.createChooser(sharingIntent, "share file with"));
  }

  void handleConference(final RemoteMessage remoteMessage) {
      String conferenceUuid = remoteMessage.getData().get("conferenceUuid");
      if (conferenceUuid != null) {
          try {
              // Intent intent = new Intent("ACROPLIA_CONFERENCE_START");
              // Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
              Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
              if (intent == null) {
                intent = new Intent(Intent.ACTION_VIEW);
              }
              // intent.setAction(Intent.ACTION_VIEW);
              // intent.setData(Uri.parse("acroplia://mobile/conference?uuid=" + conferenceUuid));
              // intent.addCategory(Intent.CATEGORY_LAUNCHER);
              intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            backgroundContext.startActivity(intent);

          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }

So, method startTextIntent is just a stub function that helps me to detect if intent is working. The problem is, when application is in foreground - text intent starts. But when application is in background it doesn't. Method handleConference also doesn't work. I also tried to add intent filter. It didn't help.

<intent-filter android:label="acroplia_conference">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data
        android:host="mobile"
        android:pathPrefix="/conference"
        android:scheme="acroplia" />
</intent-filter>

What I'm doing wrong? I also see messages about text intent:

I/Timeline( 6311): Timeline: Activity_launch_request time:584919670 intent:Intent { act=android.intent.action.CHOOSER (has extras) }
I/Timeline( 6311): Timeline: Activity_launch_request time:584919681 intent:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.gxbventures.acroplia.dev cmp=com.gxbventures.acroplia.dev/com.gxbventures.acroplia.MainActivity }

Can somebody tell me, why intent is not started from background?

android
flutter
service
background
firebase-cloud-messaging
asked on Stack Overflow May 18, 2020 by Niyaz Ginatullin • edited May 19, 2020 by sergiy tikhonov

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0