-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Hi,
My app uses firebase cloud messaging for push notifications. Unfortunately, onLaunch is called too often.
You’ll find an example app to reproduce the issue below (unfortunately, you have to follow all steps to configure the messaging plugin like state here (https://pub.dev/packages/firebase_messaging#-readme-tab-) before you can reproduce the issue).
Following scenario:
The app has two pages. If a push notification is received the app should open page 2. On Page two is a button to go back to page 1. After the user clicks on this button, the app is expected to be on page 1. This works fine, if the app wasn’t terminated when the notification was received. Alas, if it was terminated, the app opens page 1 just for a few minutes and jumps forward to page 2 again.
Steps to reproduce:
- Start the app and note the
- Terminate the app
- Send a cloud push message to the noted token
- --> A notification should appear
- Click on the notification
- --> Apps opens page 2
- Click Button “Back to page 1”
- --> <Expected:> App is on page 1
- --> <Actual:> App is on page 1 for a few milliseconds and opens page 2 again
Logs
Unfortunately, logs aren’t possible because is only happens, if the app was terminated previously…
C:\Users\x\Projekte\flutter_test\notification\flutter_app>flutter doctor -v
[√] Flutter (Channel beta, v1.5.4-hotfix.2, on Microsoft Windows [Version 10.0.17763.437], locale de-DE)
• Flutter version 1.5.4-hotfix.2 at C:\Dev\flutter
• Framework revision 7a4c33425d (2 weeks ago), 2019-04-29 11:05:24 -0700
• Engine revision 52c7a1e849
• Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:\Users\x\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
• All Android licenses accepted.
[√] Android Studio (version 3.4)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 35.3.1
• Dart plugin version 183.6270
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
[√] Connected device (1 available)
• Pixel 2 • xxxxx • android-arm64 • Android 9 (API 28)
Example App:
import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
_firebaseMessaging.getToken().then((token) {
print(token);
});
_firebaseMessaging.configure(
onMessage: (Map<dynamic, dynamic> message) => _redirectToPageTwo(),
onResume: (Map<dynamic, dynamic> message) => _redirectToPageTwo(),
onLaunch: (Map<dynamic, dynamic> message) => _redirectToPageTwo(),
);
super.initState();
}
_redirectToPageTwo(){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => PageTwo()));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Page1'),),
);
}
}
class PageTwo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Center(child: Text('Page2'),),
RaisedButton(child: Text('Go back to page 1'), onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => MyHomePage())),)
],
),
);
}
}name: flutter_app
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
firebase_core: ^0.4.0
firebase_messaging: 5.0.0
flutter:
uses-material-design: true