Skip to content

Commit d195f9f

Browse files
authored
[flutter_local_notifications] fix bug with calling getNotificationAppLaunchDetails() on Android (#515)
1 parent aa3c384 commit d195f9f

File tree

5 files changed

+93
-52
lines changed

5 files changed

+93
-52
lines changed

flutter_local_notifications/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# [1.2.1]
2+
3+
* [Android] Fixed issue [512](https://github.com/MaikuB/flutter_local_notifications/issues/512) where calling `getNotificationAppLaunchDetails()` within the `onSelectNotification` callback could indicating that the app was launched by tapping on a notification when it wasn't the case
4+
* Update example app to indicate if a notification launched the app and include the launch notification payload
5+
16
# [1.2.0+4]
27

38
* Title at the top of the readme is now the same name as the plugin

flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import android.text.Spanned;
2020

2121
import androidx.annotation.NonNull;
22-
import androidx.core.app.NotificationCompat;
2322
import androidx.core.app.AlarmManagerCompat;
23+
import androidx.core.app.NotificationCompat;
2424
import androidx.core.app.NotificationManagerCompat;
2525
import androidx.core.app.Person;
2626
import androidx.core.graphics.drawable.IconCompat;
@@ -95,11 +95,11 @@ public class FlutterLocalNotificationsPlugin implements MethodCallHandler, Plugi
9595
static String NOTIFICATION = "notification";
9696
static String NOTIFICATION_DETAILS = "notificationDetails";
9797
static String REPEAT = "repeat";
98+
static Gson gson;
9899
private MethodChannel channel;
99100
private Context applicationContext;
100101
private Activity mainActivity;
101-
static Gson gson;
102-
102+
private boolean initialized;
103103

104104
public static void registerWith(Registrar registrar) {
105105
FlutterLocalNotificationsPlugin plugin = new FlutterLocalNotificationsPlugin();
@@ -108,47 +108,6 @@ public static void registerWith(Registrar registrar) {
108108
plugin.onAttachedToEngine(registrar.context(), registrar.messenger());
109109
}
110110

111-
private void setActivity(Activity flutterActivity) {
112-
this.mainActivity = flutterActivity;
113-
}
114-
115-
private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) {
116-
this.applicationContext = context;
117-
this.channel = new MethodChannel(binaryMessenger, METHOD_CHANNEL);
118-
this.channel.setMethodCallHandler(this);
119-
}
120-
121-
@Override
122-
public void onAttachedToEngine(FlutterPluginBinding binding) {
123-
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
124-
}
125-
126-
@Override
127-
public void onDetachedFromEngine(FlutterPluginBinding binding) {
128-
}
129-
130-
@Override
131-
public void onAttachedToActivity(ActivityPluginBinding binding) {
132-
binding.addOnNewIntentListener(this);
133-
mainActivity = binding.getActivity();
134-
}
135-
136-
@Override
137-
public void onDetachedFromActivityForConfigChanges() {
138-
this.mainActivity = null;
139-
}
140-
141-
@Override
142-
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
143-
binding.addOnNewIntentListener(this);
144-
mainActivity = binding.getActivity();
145-
}
146-
147-
@Override
148-
public void onDetachedFromActivity() {
149-
this.mainActivity = null;
150-
}
151-
152111
static void rescheduleNotifications(Context context) {
153112
ArrayList<NotificationDetails> scheduledNotifications = loadScheduledNotifications(context);
154113
for (NotificationDetails scheduledNotification : scheduledNotifications) {
@@ -243,7 +202,6 @@ private static ArrayList<NotificationDetails> loadScheduledNotifications(Context
243202
return scheduledNotifications;
244203
}
245204

246-
247205
private static void saveScheduledNotifications(Context context, ArrayList<NotificationDetails> scheduledNotifications) {
248206
Gson gson = buildGson();
249207
String json = gson.toJson(scheduledNotifications);
@@ -699,6 +657,47 @@ private static NotificationManagerCompat getNotificationManager(Context context)
699657
return NotificationManagerCompat.from(context);
700658
}
701659

660+
private void setActivity(Activity flutterActivity) {
661+
this.mainActivity = flutterActivity;
662+
}
663+
664+
private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) {
665+
this.applicationContext = context;
666+
this.channel = new MethodChannel(binaryMessenger, METHOD_CHANNEL);
667+
this.channel.setMethodCallHandler(this);
668+
}
669+
670+
@Override
671+
public void onAttachedToEngine(FlutterPluginBinding binding) {
672+
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
673+
}
674+
675+
@Override
676+
public void onDetachedFromEngine(FlutterPluginBinding binding) {
677+
}
678+
679+
@Override
680+
public void onAttachedToActivity(ActivityPluginBinding binding) {
681+
binding.addOnNewIntentListener(this);
682+
mainActivity = binding.getActivity();
683+
}
684+
685+
@Override
686+
public void onDetachedFromActivityForConfigChanges() {
687+
this.mainActivity = null;
688+
}
689+
690+
@Override
691+
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
692+
binding.addOnNewIntentListener(this);
693+
mainActivity = binding.getActivity();
694+
}
695+
696+
@Override
697+
public void onDetachedFromActivity() {
698+
this.mainActivity = null;
699+
}
700+
702701
@Override
703702
public void onMethodCall(MethodCall call, Result result) {
704703
switch (call.method) {
@@ -790,7 +789,7 @@ private void show(MethodCall call, Result result) {
790789
private void getNotificationAppLaunchDetails(Result result) {
791790
Map<String, Object> notificationAppLaunchDetails = new HashMap<>();
792791
String payload = null;
793-
Boolean notificationLaunchedApp = (mainActivity != null && SELECT_NOTIFICATION.equals(mainActivity.getIntent().getAction()));
792+
Boolean notificationLaunchedApp = !initialized && mainActivity != null && SELECT_NOTIFICATION.equals(mainActivity.getIntent().getAction());
794793
notificationAppLaunchDetails.put(NOTIFICATION_LAUNCHED_APP, notificationLaunchedApp);
795794
if (notificationLaunchedApp) {
796795
payload = mainActivity.getIntent().getStringExtra(PAYLOAD);
@@ -813,6 +812,7 @@ private void initialize(MethodCall call, Result result) {
813812
if (mainActivity != null) {
814813
sendNotificationPayloadMessage(mainActivity.getIntent());
815814
}
815+
initialized = true;
816816
result.success(true);
817817
}
818818

flutter_local_notifications/example/lib/main.dart

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final BehaviorSubject<ReceivedNotification> didReceiveLocalNotificationSubject =
2121
final BehaviorSubject<String> selectNotificationSubject =
2222
BehaviorSubject<String>();
2323

24+
NotificationAppLaunchDetails notificationAppLaunchDetails;
25+
2426
class ReceivedNotification {
2527
final int id;
2628
final String title;
@@ -39,10 +41,9 @@ class ReceivedNotification {
3941
Future<void> main() async {
4042
// needed if you intend to initialize in the `main` function
4143
WidgetsFlutterBinding.ensureInitialized();
42-
// NOTE: if you want to find out if the app was launched via notification then you could use the following call and then do something like
43-
// change the default route of the app
44-
// var notificationAppLaunchDetails =
45-
// await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
44+
45+
notificationAppLaunchDetails =
46+
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
4647

4748
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
4849
// Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
@@ -182,6 +183,41 @@ class _HomePageState extends State<HomePage> {
182183
child: Text(
183184
'Tap on a notification when it appears to trigger navigation'),
184185
),
186+
Padding(
187+
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
188+
child: Text.rich(
189+
TextSpan(
190+
children: [
191+
TextSpan(
192+
text: 'Did notification launch app? ',
193+
style: TextStyle(fontWeight: FontWeight.bold),
194+
),
195+
TextSpan(
196+
text:
197+
'${notificationAppLaunchDetails?.didNotificationLaunchApp ?? false}',
198+
)
199+
],
200+
),
201+
),
202+
),
203+
if (notificationAppLaunchDetails?.didNotificationLaunchApp ??
204+
false)
205+
Padding(
206+
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
207+
child: Text.rich(
208+
TextSpan(
209+
children: [
210+
TextSpan(
211+
text: 'Launch notification payload: ',
212+
style: TextStyle(fontWeight: FontWeight.bold),
213+
),
214+
TextSpan(
215+
text: notificationAppLaunchDetails.payload,
216+
)
217+
],
218+
),
219+
),
220+
),
185221
PaddedRaisedButton(
186222
buttonText: 'Show plain notification with payload',
187223
onPressed: () async {

flutter_local_notifications/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ flutter:
2525
uses-material-design: true
2626

2727
environment:
28-
sdk: ">=2.0.0-dev.28.0 <3.0.0"
28+
sdk: ">=2.3.0 <3.0.0"
2929
flutter: ">=1.12.13+hotfix.5"
3030

flutter_local_notifications/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_local_notifications
22
description: A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.
3-
version: 1.2.0+4
3+
version: 1.2.1
44
homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications
55

66
dependencies:

0 commit comments

Comments
 (0)