Skip to content

Commit

Permalink
submit notification payload in action callback as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ened committed Jan 2, 2021
1 parent 54b91a5 commit c21f8fc
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 7 deletions.
Expand Up @@ -4,7 +4,10 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.Action;
import androidx.core.app.RemoteInput;
import com.dexterous.flutterlocalnotifications.isolate.IsolatePreferences;
import io.flutter.embedding.engine.FlutterEngine;
Expand Down Expand Up @@ -35,6 +38,8 @@ public void onReceive(Context context, Intent intent) {
final Map<String, Object> action = new HashMap<>();
action.put("id", id);

action.put("payload", intent.hasExtra("payload") ? intent.getStringExtra("payload") : "");

Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
action.put("input", remoteInput.getString("FlutterLocalNotificationsPluginInputResult"));
Expand Down
Expand Up @@ -19,6 +19,7 @@
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import android.text.Html;
import android.text.Spanned;
Expand Down Expand Up @@ -190,11 +191,12 @@ private static Notification createNotification(Context context, NotificationDeta
}

Intent actionIntent = new Intent(context, ActionBroadcastReceiver.class)
.setAction(ActionBroadcastReceiver.ACTION_TAPPED).putExtra("id", action.id);
.setAction(ActionBroadcastReceiver.ACTION_TAPPED)
.putExtra("id", action.id)
.putExtra(PAYLOAD, notificationDetails.payload);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, requestCode++, actionIntent, 0);
Builder actionBuilder = new Builder(icon, action.title, actionPendingIntent);


if (action.contextual != null) {
actionBuilder.setContextual(action.contextual);
}
Expand Down
6 changes: 3 additions & 3 deletions flutter_local_notifications/example/lib/main.dart
Expand Up @@ -42,10 +42,10 @@ class ReceivedNotification {
final String payload;
}

void notificationTapBackground(String id, String input) {
void notificationTapBackground(String id, String input, String payload) {
// ignore: avoid_print
print('notification action tapped: $id');
if (input != null) {
print('notification action tapped: $id with payload: $payload');
if (input.isNotEmpty) {
// ignore: avoid_print
print('notification action tapped with input: $input');
}
Expand Down
Expand Up @@ -902,6 +902,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
[actionEventSink addItem:@{
@"id": response.actionIdentifier,
@"input": text,
@"payload": response.notification.request.content.userInfo[@"payload"],
}];


Expand Down
Expand Up @@ -27,7 +27,7 @@ void callbackDispatcher() {
.map<Map<String, dynamic>>(
(Map<dynamic, dynamic> event) => Map.castFrom(event))
.listen((Map<String, dynamic> event) {
callback(event['id'], event['input']);
callback(event['id'], event['input'], event['payload']);
});
});
}
3 changes: 2 additions & 1 deletion flutter_local_notifications/lib/src/typedefs.dart
@@ -1,7 +1,8 @@
import 'dart:async';

/// Callback function when a notification is received.
typedef NotificationActionCallback = Function(String, String);
typedef NotificationActionCallback = Function(
String id, String input, String payload);

/// Signature of callback passed to [initialize] that is triggered when user
/// taps on a notification.
Expand Down

0 comments on commit c21f8fc

Please sign in to comment.