intercom_flutter icon indicating copy to clipboard operation
intercom_flutter copied to clipboard

Tapping on push notification restarts backgrounded app on Android

Open karim-cardeo opened this issue 2 years ago • 16 comments

Hi folks,

I have recently found an issue which occurs when tapping on a push notification sent by Intercom on Android if the app is in the background.

Steps to replicate:

Given I have an Android Flutter app with the intercom_flutter package installed and correctly setup, And I have launched the app and put it in the background, When I receive a push notification from Intercom, And I tap on the push notification, Then I am routed to the Intercom messenger, And the app is restarted.

The expected result should instead be that the app is brought back from the background without restarting.

I have been able to replicate this issue on a brand new Flutter project that uses the latest version of Flutter (3.19.2) and of the intercom_flutter package (8.1.1).

Below is the code of the only screen in my test project (sensitive data redacted):

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:intercom_flutter/intercom_flutter.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: tap,
          child: const Text('Click me'),
        ),
      ),
    );
  }

  Future<void> tap() async {
    await FirebaseMessaging.instance.requestPermission();

    await Intercom.instance.initialize('appId', androidApiKey: 'apiKey');
    await Intercom.instance.setUserHash('userHash');
    await Intercom.instance.loginIdentifiedUser(userId: 'userId');

    await Future.delayed(const Duration(milliseconds: 1500));

    final token = await FirebaseMessaging.instance.getToken();
    if (token == null) return;
    await Intercom.instance.sendTokenToIntercom(token);

    await Intercom.instance.displayMessenger();
  }
}

And here is the pubspec.yaml file which shows all of the dependencies on this project:

name: intercom_nav_test
description: "A new Flutter project."
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=3.0.5 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.6
  intercom_flutter: ^8.1.1
  firebase_core: ^2.26.0
  firebase_messaging: ^14.7.18
  flutter_native_splash: ^2.3.7

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^3.0.0
  build_runner: ^2.4.8

flutter:
  uses-material-design: true

Finally, I have attached below a video of the issue captured on my Android device (the blue screen that appears is my mock splash screen, there to indicate the app is restarting):

https://github.com/v3rm0n/intercom_flutter/assets/117079582/89aaa8cf-2eee-450e-94fc-11ac88998c0e

karim-cardeo avatar Mar 07 '24 12:03 karim-cardeo

@karim-cardeo What is the launch mode of your MainActivity in the manifest? By default, it is "standard" and with this is the expected behavior as in the video.

Try to use the launchMode as "singleTask" or "singleInstance". See https://developer.android.com/guide/topics/manifest/activity-element#lmode

deepak786 avatar Mar 07 '24 17:03 deepak786

I can reproduce the issue - so whenever push notification is clicked, Flutter app is restarted, if it was in the background. Everything is set up correctly (including Manifest). Can you please check how to fix that?

Looks like the Intent which is sent through the notification is restarting the app.

vasa137 avatar Apr 09 '24 22:04 vasa137

What is the launch mode of your MainActivity in the manifest?

deepak786 avatar Apr 10 '24 05:04 deepak786

Tried both with singleTask and singleInstance, all other features are working well, so everything's correctly configured.

vasa137 avatar Apr 10 '24 09:04 vasa137

I will test it at my end. But I suggest you to contact with the Intercom support as it seems to be an issue with SDK itself.

deepak786 avatar Apr 14 '24 17:04 deepak786

Thanks @vasa137 @deepak786 for testing the different launch modes, I have had to disable Intercom pushes for Android as a mitigation for this issue and so I was not able to test further.

It would be good to also test this in a pure Android app with the Intercom SDK directly to see if the issue is replicable there - I will try this when I have some time. If it's replicable, we can then contact Intercom support. I have found that they are quite technical so no doubt they'd be able to help out with this.

karim-cardeo avatar Apr 17 '24 11:04 karim-cardeo

We just noticed this same issue, anyone found a workaround for this? In our case the app seems to restart in continuous loop causing eventually ANR.

nikorehnback avatar May 29 '24 08:05 nikorehnback

Anyone had time to check if this reproduces with native app or is it happening only with Flutter?

nikorehnback avatar May 30 '24 17:05 nikorehnback

@nikorehnback did you try with different launch modes as explained here https://github.com/v3rm0n/intercom_flutter/issues/414#issuecomment-1984101691

deepak786 avatar Jun 03 '24 20:06 deepak786

Does your MainActivity extends FlutterActivity or FlutterFragmentActivity?

deepak786 avatar Jun 03 '24 21:06 deepak786

I use FlutterFragmentActivity, and have problems

vasa137 avatar Jun 03 '24 22:06 vasa137

Using FlutterFragmentActivity as well and did try with different launch modes too but didn't see any difference. Haven't had time unfortunately to create minimal sample to reproduce the bug we see.

nikorehnback avatar Jun 04 '24 01:06 nikorehnback

I will test the issue at my end. Meanwhile could you please report the issue to Intercom support directly as it seems to be an issue in native SDK.

deepak786 avatar Jun 04 '24 08:06 deepak786