Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onSelectNotification has been called forever on Android #751

Closed
gabf3st opened this issue Aug 11, 2020 · 20 comments · Fixed by #755
Closed

onSelectNotification has been called forever on Android #751

gabf3st opened this issue Aug 11, 2020 · 20 comments · Fixed by #755

Comments

@gabf3st
Copy link

gabf3st commented Aug 11, 2020

Describe the bug
onSelectNotification has been called forever once press Back button on device to exit and re-launch app with Recent app on Android

Tested on
Android 9
flutter_local_notifications : 1.4.4+2 and 1.5.0-beta.6

To Reproduce

  1. Launch the example app (https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications/example)
  2. Tap on Show plain notification with payload
  3. Press Back button (on device) to exit the app
  4. Tap on that notification
  5. After it navigates to the second page, press Back button until exit the app
  6. Press Recent App button
  7. Tap on the example app which will navigate to the second screen without any tap required

Expected behavior
When re-launch the app, it should not call onSelectNotification if user hasn't tap on the notification

Attachments
Here is the VDO to demonstrate the reproduce steps this issue
Link to VDO

@MaikuB
Copy link
Owner

MaikuB commented Aug 11, 2020

This not a bug with the plugin but an issue in the example app. Back button doesn't exit the app, it only sends it to the background so you're actually resuming the app when you pick the recent app. State management and dealing with the app lifecycle will in these scenarios is up for developers to implement. For example, you might track the id of the last processed notification and this isn't done for the example as every notification has the same id to make it easier to demonstrate cancelling/removing a notification

Edit: I'll leave some notes on this in an upcoming update

@MaikuB MaikuB closed this as completed Aug 11, 2020
@MaikuB
Copy link
Owner

MaikuB commented Aug 12, 2020

My bad, I'm reopening this as this turned out not to do with the Flutter lifecycle. Would you be able to assist by testing the android_check_launched_from_history branch? You appear to have a Samsung device so want to make sure there isn't some weird quirk specific to Samsung (I've got a Pixel 4 XL)

@MaikuB MaikuB reopened this Aug 12, 2020
@gabf3st
Copy link
Author

gabf3st commented Aug 13, 2020

After tested the android_check_launched_from_history on Samsung J7, the issue has resolved with nothing weird.
I appreciate for your kindly support.

@matamune94
Copy link

matamune94 commented Aug 13, 2020

Hi @MaikuB , I have the same problem, I use sony ultra XA2, when I press the button restart onSelectNotification app is called automatically

@MaikuB
Copy link
Owner

MaikuB commented Aug 13, 2020

@matamune94 there's already a fix for it in the branch I mentioned in this issue. Please try it out and confirm it solves the issue for you so I can see about merging it in and releasing the fix

@matamune94
Copy link

So bad, i have error when add this plugin

Running "flutter pub get" in tes...                             
Could not find a file named "pubspec.yaml" in https://github.com/MaikuB/flutter_local_notifications.git ea3e5d78413c6580b50c1d6115ae4038b795caec.
pub get failed (1; Could not find a file named "pubspec.yaml" in https://github.com/MaikuB/flutter_local_notifications.git ea3e5d78413c6580b50c1d6115ae4038b795caec.)
exit code 1

I try fix it, i will report when test finished . thanks @MaikuB

@MaikuB
Copy link
Owner

MaikuB commented Aug 13, 2020

The plugin is not at the root of the repository so you need to fix your pubspec file to make sure you specify the path of the plugin. Please read the docs at https://dart.dev/tools/pub/dependencies#git-packages for more info

@matamune94
Copy link

@MaikuB something wrong ?

dependencies:
  flutter:
    sdk: flutter
  flutter_local_notifications:
    git:
      url: https://github.com/MaikuB/flutter_local_notifications.git
      ref: android_check_launched_from_history

@MaikuB
Copy link
Owner

MaikuB commented Aug 13, 2020

Yes, you didn't specify the path

@matamune94
Copy link

@MaikuB I tesed it not working .

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(fontFamily: 'OpenSans'),
      home: HomeScreen()
    );
  }

}

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
  @override
  void initState() {
    // TODO: implement initState
    _initNotification();
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your channel id', 'your channel name', 'your channel description',
        importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
    var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    Future.delayed(Duration(seconds: 5), () async {
      await flutterLocalNotificationsPlugin.show(
        0,
        'title',
        'body',
        platformChannelSpecifics,
        payload: 'payload'
      );
    });
    super.initState();
  }

  _initNotification() async {
    var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
      // Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
      // of the `IOSFlutterLocalNotificationsPlugin` class
    var initializationSettingsIOS = IOSInitializationSettings();
    var initializationSettings = InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);

    await flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: (dynamic payload) async {
      print(['what the hell with payload', payload]);
      if(payload != null){
        _pushToOtherScreen();
      }
      // return _initLocalNotification(payload);
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
      child: RaisedButton(
      child: Text('data'),
      onPressed: (){
        _pushToOtherScreen();
      }),
      ),
    );
  }

  _pushToOtherScreen(){
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (BuildContext context) {
      return OtherScreen();
    }));
  }
}

class OtherScreen extends StatefulWidget {
  OtherScreen({Key key}) : super(key: key);

  @override
  _OtherScreenState createState() => _OtherScreenState();
}

class _OtherScreenState extends State<OtherScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
       child: Text('other screen'),
      ),
    );
  }
}

@MaikuB
Copy link
Owner

MaikuB commented Aug 13, 2020

I tesed it not working

@matamune94 Please describe in more detail

@gabf3st
Copy link
Author

gabf3st commented Aug 13, 2020

@matamune94 You need to specify the location of the plugin since it didn't locate at the root of https://github.com/MaikuB/flutter_local_notifications.git

To specify a different location in the repo, use the path argument:

  flutter_local_notifications:
    git:
      url: https://github.com/MaikuB/flutter_local_notifications.git
      path: flutter_local_notifications
      ref: android_check_launched_from_history

@matamune94
Copy link

@matamune94 You need to specify the location of the plugin since it didn't locate at the root of https://github.com/MaikuB/flutter_local_notifications.git

To specify a different location in the repo, use the path argument:

  flutter_local_notifications:
    git:
      url: https://github.com/MaikuB/flutter_local_notifications.git
      path: flutter_local_notifications
      ref: android_check_launched_from_history

Yeb plugin installed.

@matamune94
Copy link

I tesed it not working

@matamune94 Please describe in more detail

I first launch app go to homeScreen and after 2 seconds it will send a notification, when i click on notification it will push to other screen, yeb it's good. But when re-launch app the screen automatically switches to otherScreen instead of the homeScreen

@matamune94
Copy link

Hi @gabf3st are you tested ?

@MaikuB
Copy link
Owner

MaikuB commented Aug 13, 2020

@matamune94 could you please record a video of the steps you've described so I can be sure that your issue is the same as what's been reported earlier here. Also I'd suggest running flutter clean before trying again. If you run the example app in the branch described, the issue doesn't exist anymore so I suspect the issue lies elsewhere in your app

@matamune94
Copy link

matamune94 commented Aug 13, 2020

@MaikuB
Copy link
Owner

MaikuB commented Aug 13, 2020

@matamune94 the scenario you described is different than what was reported by @gabf3st The scenario being reported here is if the app is sent to the background and the Android task/app switcher is used to resume the app.

From the video, it looks like you hot restarted the app, and there's likely nothing I can do about that scenario because it's to do with the way the Flutter tooling works. Furthermore it's not a scenario that would happen to users as a hot restart is not the same process as a when a user restarts the app

@matamune94
Copy link

matamune94 commented Aug 13, 2020

@MaikuB oh, I seem to have misunderstood, but this error is really an inconvenience in debugging the app, I hope you can fix it. thanks you supported

@MaikuB
Copy link
Owner

MaikuB commented Aug 13, 2020

That would require changes in how Flutter works so not something I can address . I doubt the team can do anything about it either as it related to how Android works in tracking what intent triggered an activity. That info is only cleared when the app has been completed killed/terminated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants