Skip to content

Commit

Permalink
[flutter_local_notifications] fix bug with calling getNotificationApp…
Browse files Browse the repository at this point in the history
…LaunchDetails() on Android (#515)
  • Loading branch information
MaikuB authored Mar 5, 2020
1 parent aa3c384 commit d195f9f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 52 deletions.
5 changes: 5 additions & 0 deletions flutter_local_notifications/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# [1.2.1]

* [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
* Update example app to indicate if a notification launched the app and include the launch notification payload

# [1.2.0+4]

* Title at the top of the readme is now the same name as the plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import android.text.Spanned;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.AlarmManagerCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person;
import androidx.core.graphics.drawable.IconCompat;
Expand Down Expand Up @@ -95,11 +95,11 @@ public class FlutterLocalNotificationsPlugin implements MethodCallHandler, Plugi
static String NOTIFICATION = "notification";
static String NOTIFICATION_DETAILS = "notificationDetails";
static String REPEAT = "repeat";
static Gson gson;
private MethodChannel channel;
private Context applicationContext;
private Activity mainActivity;
static Gson gson;

private boolean initialized;

public static void registerWith(Registrar registrar) {
FlutterLocalNotificationsPlugin plugin = new FlutterLocalNotificationsPlugin();
Expand All @@ -108,47 +108,6 @@ public static void registerWith(Registrar registrar) {
plugin.onAttachedToEngine(registrar.context(), registrar.messenger());
}

private void setActivity(Activity flutterActivity) {
this.mainActivity = flutterActivity;
}

private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) {
this.applicationContext = context;
this.channel = new MethodChannel(binaryMessenger, METHOD_CHANNEL);
this.channel.setMethodCallHandler(this);
}

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
binding.addOnNewIntentListener(this);
mainActivity = binding.getActivity();
}

@Override
public void onDetachedFromActivityForConfigChanges() {
this.mainActivity = null;
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
binding.addOnNewIntentListener(this);
mainActivity = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
this.mainActivity = null;
}

static void rescheduleNotifications(Context context) {
ArrayList<NotificationDetails> scheduledNotifications = loadScheduledNotifications(context);
for (NotificationDetails scheduledNotification : scheduledNotifications) {
Expand Down Expand Up @@ -243,7 +202,6 @@ private static ArrayList<NotificationDetails> loadScheduledNotifications(Context
return scheduledNotifications;
}


private static void saveScheduledNotifications(Context context, ArrayList<NotificationDetails> scheduledNotifications) {
Gson gson = buildGson();
String json = gson.toJson(scheduledNotifications);
Expand Down Expand Up @@ -699,6 +657,47 @@ private static NotificationManagerCompat getNotificationManager(Context context)
return NotificationManagerCompat.from(context);
}

private void setActivity(Activity flutterActivity) {
this.mainActivity = flutterActivity;
}

private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) {
this.applicationContext = context;
this.channel = new MethodChannel(binaryMessenger, METHOD_CHANNEL);
this.channel.setMethodCallHandler(this);
}

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
binding.addOnNewIntentListener(this);
mainActivity = binding.getActivity();
}

@Override
public void onDetachedFromActivityForConfigChanges() {
this.mainActivity = null;
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
binding.addOnNewIntentListener(this);
mainActivity = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
this.mainActivity = null;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
Expand Down Expand Up @@ -790,7 +789,7 @@ private void show(MethodCall call, Result result) {
private void getNotificationAppLaunchDetails(Result result) {
Map<String, Object> notificationAppLaunchDetails = new HashMap<>();
String payload = null;
Boolean notificationLaunchedApp = (mainActivity != null && SELECT_NOTIFICATION.equals(mainActivity.getIntent().getAction()));
Boolean notificationLaunchedApp = !initialized && mainActivity != null && SELECT_NOTIFICATION.equals(mainActivity.getIntent().getAction());
notificationAppLaunchDetails.put(NOTIFICATION_LAUNCHED_APP, notificationLaunchedApp);
if (notificationLaunchedApp) {
payload = mainActivity.getIntent().getStringExtra(PAYLOAD);
Expand All @@ -813,6 +812,7 @@ private void initialize(MethodCall call, Result result) {
if (mainActivity != null) {
sendNotificationPayloadMessage(mainActivity.getIntent());
}
initialized = true;
result.success(true);
}

Expand Down
44 changes: 40 additions & 4 deletions flutter_local_notifications/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final BehaviorSubject<ReceivedNotification> didReceiveLocalNotificationSubject =
final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();

NotificationAppLaunchDetails notificationAppLaunchDetails;

class ReceivedNotification {
final int id;
final String title;
Expand All @@ -39,10 +41,9 @@ class ReceivedNotification {
Future<void> main() async {
// needed if you intend to initialize in the `main` function
WidgetsFlutterBinding.ensureInitialized();
// 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
// change the default route of the app
// var notificationAppLaunchDetails =
// await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();

notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();

var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
// Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
Expand Down Expand Up @@ -182,6 +183,41 @@ class _HomePageState extends State<HomePage> {
child: Text(
'Tap on a notification when it appears to trigger navigation'),
),
Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: Text.rich(
TextSpan(
children: [
TextSpan(
text: 'Did notification launch app? ',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(
text:
'${notificationAppLaunchDetails?.didNotificationLaunchApp ?? false}',
)
],
),
),
),
if (notificationAppLaunchDetails?.didNotificationLaunchApp ??
false)
Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: Text.rich(
TextSpan(
children: [
TextSpan(
text: 'Launch notification payload: ',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(
text: notificationAppLaunchDetails.payload,
)
],
),
),
),
PaddedRaisedButton(
buttonText: 'Show plain notification with payload',
onPressed: () async {
Expand Down
2 changes: 1 addition & 1 deletion flutter_local_notifications/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ flutter:
uses-material-design: true

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

2 changes: 1 addition & 1 deletion flutter_local_notifications/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_local_notifications
description: A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.
version: 1.2.0+4
version: 1.2.1
homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications

dependencies:
Expand Down

0 comments on commit d195f9f

Please sign in to comment.