Skip to content

Commit

Permalink
Merge pull request MaikuB#2 from Kavantix/17-notification-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ened committed Nov 26, 2021
2 parents 5970064 + 8590b1a commit b2ce3a2
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 23 deletions.
Expand Up @@ -22,18 +22,20 @@
public class ActionBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_TAPPED =
"com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver.ACTION_TAPPED";
public static final String ACTION_ID = "actionId";
public static final String NOTIFICATION_ID = "notificationId";

@Nullable private static ActionEventSink actionEventSink;

@Nullable private static FlutterEngine engine;

@Override
public void onReceive(Context context, Intent intent) {
final String id = intent.getStringExtra("id");

final Map<String, Object> action = new HashMap<>();
action.put("id", id);

action.put("notificationId", intent.getIntExtra(NOTIFICATION_ID, -1));
action.put(
"actionId", intent.hasExtra(ACTION_ID) ? intent.getStringExtra(ACTION_ID) : "unknown");
action.put("payload", intent.hasExtra("payload") ? intent.getStringExtra("payload") : "");

Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
Expand Down
Expand Up @@ -231,10 +231,12 @@ protected static Notification createNotification(
Intent actionIntent =
new Intent(context, ActionBroadcastReceiver.class)
.setAction(ActionBroadcastReceiver.ACTION_TAPPED)
.putExtra("id", action.id)
.putExtra(ActionBroadcastReceiver.NOTIFICATION_ID, notificationDetails.id)
.putExtra(ActionBroadcastReceiver.ACTION_ID, action.id)
.putExtra(PAYLOAD, notificationDetails.payload);
PendingIntent actionPendingIntent =
PendingIntent.getBroadcast(context, requestCode++, actionIntent, 0);
PendingIntent.getBroadcast(
context, requestCode++, actionIntent, PendingIntent.FLAG_ONE_SHOT);
Builder actionBuilder = new Builder(icon, action.title, actionPendingIntent);

if (action.contextual != null) {
Expand Down
14 changes: 7 additions & 7 deletions flutter_local_notifications/example/lib/main.dart
Expand Up @@ -48,12 +48,12 @@ class ReceivedNotification {

String? selectedNotificationPayload;

void notificationTapBackground(String id, String? input, String? payload) {
// ignore: avoid_print
print('notification action tapped: $id with payload: $payload');
if (input?.isNotEmpty ?? false) {
void notificationTapBackground(NotificationActionDetails details) {
print(
'notification(${details.id}) action tapped: ${details.actionId} with payload: ${details.payload}');
if (details.input?.isNotEmpty ?? false) {
// ignore: avoid_print
print('notification action tapped with input: $input');
print('notification action tapped with input: ${details.input}');
}
}

Expand Down Expand Up @@ -915,8 +915,8 @@ class _HomePageState extends State<HomePage> {
iOS: iosNotificationDetails,
);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item x');
112, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item z');
}

Future<void> _showNotificationWithTextAction() async {
Expand Down
10 changes: 6 additions & 4 deletions flutter_local_notifications/example/pubspec.yaml
Expand Up @@ -9,10 +9,6 @@ dependencies:
sdk: flutter
flutter_local_notifications:
path: ../
flutter_local_notifications_platform_interface:
path: ../../flutter_local_notifications_platform_interface/
flutter_local_notifications_linux:
path: ../../flutter_local_notifications_linux/
flutter_native_timezone: ^2.0.0
http: ^0.13.4
image: ^3.0.8
Expand All @@ -28,6 +24,12 @@ dev_dependencies:
integration_test:
sdk: flutter

dependency_overrides:
flutter_local_notifications_platform_interface:
path: ../../flutter_local_notifications_platform_interface/
flutter_local_notifications_linux:
path: ../../flutter_local_notifications_linux/

flutter:
uses-material-design: true
assets:
Expand Down
Expand Up @@ -1091,7 +1091,8 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
}

[actionEventSink addItem:@{
@"id" : response.actionIdentifier,
@"notificationId" : response.notification.request.identifier,
@"actionId" : response.actionIdentifier,
@"input" : text,
@"payload" : response.notification.request.content.userInfo[@"payload"],
}];
Expand Down
Expand Up @@ -4,7 +4,8 @@ export 'package:flutter_local_notifications_platform_interface/flutter_local_not
SelectNotificationCallback,
PendingNotificationRequest,
RepeatInterval,
NotificationAppLaunchDetails;
NotificationAppLaunchDetails,
NotificationActionDetails;

export 'src/flutter_local_notifications_plugin.dart';
export 'src/initialization_settings.dart';
Expand Down
16 changes: 15 additions & 1 deletion flutter_local_notifications/lib/src/callback_dispatcher.dart
Expand Up @@ -28,7 +28,21 @@ void callbackDispatcher() {
.map<Map<String, dynamic>>(
(Map<dynamic, dynamic> event) => Map.castFrom(event))
.listen((Map<String, dynamic> event) {
callback?.call(event['id'], event['input'], event['payload']);
final Object notificationId = event['notificationId'];
final int id;
if (notificationId is int) {
id = notificationId;
} else if (notificationId is String) {
id = int.parse(notificationId);
} else {
id = -1;
}
callback?.call(NotificationActionDetails(
id: id,
actionId: event['actionId'],
input: event['input'],
payload: event['payload'],
));
});
});
}
2 changes: 1 addition & 1 deletion flutter_local_notifications/pubspec.yaml
Expand Up @@ -10,7 +10,7 @@ dependencies:
flutter:
sdk: flutter
flutter_local_notifications_linux: ^0.3.0
flutter_local_notifications_platform_interface: ^5.0.0
flutter_local_notifications_platform_interface: ^6.0.0
timezone: ^0.8.0

dev_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion flutter_local_notifications_linux/pubspec.yaml
Expand Up @@ -6,7 +6,7 @@ homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flut
dependencies:
flutter:
sdk: flutter
flutter_local_notifications_platform_interface: ^5.0.0
flutter_local_notifications_platform_interface: ^6.0.0
dbus: ^0.5.0
path: ^1.8.0
xdg_directories: ^0.2.0
Expand Down
@@ -1,7 +1,10 @@
import 'types.dart';

/// Signature of callback passed to [initialize] that is triggered when user
/// taps on a notification.
typedef SelectNotificationCallback = void Function(String? payload);

/// Callback function when a notification is received.
typedef NotificationActionCallback = void Function(
String id, String? input, String? payload);
NotificationActionDetails details,
);
23 changes: 23 additions & 0 deletions flutter_local_notifications_platform_interface/lib/src/types.dart
Expand Up @@ -31,3 +31,26 @@ class PendingNotificationRequest {
/// The notification's payload.
final String? payload;
}

/// Details of a Notification Action that was triggered.
class NotificationActionDetails {
/// Constructs an instance of [NotificationActionDetails]
NotificationActionDetails({
required this.id,
required this.actionId,
required this.input,
required this.payload,
});

/// The notification's id.
final int id;

/// The id of the action that was triggered.
final String actionId;

/// The value of the input field if the notification action had an input field.
final String? input;

/// The notification's payload
final String? payload;
}
@@ -1,6 +1,6 @@
name: flutter_local_notifications_platform_interface
description: A common platform interface for the flutter_local_notifications plugin.
version: 5.0.0
version: 6.0.0
homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications_platform_interface

environment:
Expand Down

0 comments on commit b2ce3a2

Please sign in to comment.