feat: Allow attaching arbitrary data to the notification

This commit is contained in:
PapaTutuWawa 2023-07-29 15:32:33 +02:00
parent 6da35cd0ba
commit c7ee2b6c6e
6 changed files with 84 additions and 4 deletions

View File

@ -61,7 +61,7 @@ class MyAppState extends State<MyApp> {
);
MoxplatformPlugin.notifications.getEventStream().listen((event) {
print('NotificationEvent(type: ${event.type}, jid: ${event.jid}, payload: ${event.payload})');
print('NotificationEvent(type: ${event.type}, jid: ${event.jid}, payload: ${event.payload}, extras: ${event.extra})');
});
}
@ -191,6 +191,11 @@ class MyHomePage extends StatelessWidget {
messages: messages,
channelId: channelId,
jid: 'testjid',
extra: {
'jid': 'testjid',
'avatarPath': 'lol',
'rio': 'cute',
},
),
);
},

View File

@ -387,6 +387,17 @@ public class Api {
this.messages = setterArg;
}
/** Additional data to include. */
private @Nullable Map<String, String> extra;
public @Nullable Map<String, String> getExtra() {
return extra;
}
public void setExtra(@Nullable Map<String, String> setterArg) {
this.extra = setterArg;
}
/** Constructor is non-public to enforce null safety; use Builder. */
MessagingNotification() {}
@ -427,6 +438,13 @@ public class Api {
return this;
}
private @Nullable Map<String, String> extra;
public @NonNull Builder setExtra(@Nullable Map<String, String> setterArg) {
this.extra = setterArg;
return this;
}
public @NonNull MessagingNotification build() {
MessagingNotification pigeonReturn = new MessagingNotification();
pigeonReturn.setTitle(title);
@ -434,18 +452,20 @@ public class Api {
pigeonReturn.setChannelId(channelId);
pigeonReturn.setJid(jid);
pigeonReturn.setMessages(messages);
pigeonReturn.setExtra(extra);
return pigeonReturn;
}
}
@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(5);
ArrayList<Object> toListResult = new ArrayList<Object>(6);
toListResult.add(title);
toListResult.add(id);
toListResult.add(channelId);
toListResult.add(jid);
toListResult.add(messages);
toListResult.add(extra);
return toListResult;
}
@ -461,6 +481,8 @@ public class Api {
pigeonResult.setJid((String) jid);
Object messages = list.get(4);
pigeonResult.setMessages((List<NotificationMessage>) messages);
Object extra = list.get(5);
pigeonResult.setExtra((Map<String, String>) extra);
return pigeonResult;
}
}
@ -660,6 +682,17 @@ public class Api {
this.payload = setterArg;
}
/** Extra data. Only set when type == NotificationType.reply. */
private @Nullable Map<String, String> extra;
public @Nullable Map<String, String> getExtra() {
return extra;
}
public void setExtra(@Nullable Map<String, String> setterArg) {
this.extra = setterArg;
}
/** Constructor is non-public to enforce null safety; use Builder. */
NotificationEvent() {}
@ -686,21 +719,30 @@ public class Api {
return this;
}
private @Nullable Map<String, String> extra;
public @NonNull Builder setExtra(@Nullable Map<String, String> setterArg) {
this.extra = setterArg;
return this;
}
public @NonNull NotificationEvent build() {
NotificationEvent pigeonReturn = new NotificationEvent();
pigeonReturn.setJid(jid);
pigeonReturn.setType(type);
pigeonReturn.setPayload(payload);
pigeonReturn.setExtra(extra);
return pigeonReturn;
}
}
@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(3);
ArrayList<Object> toListResult = new ArrayList<Object>(4);
toListResult.add(jid);
toListResult.add(type == null ? null : type.index);
toListResult.add(payload);
toListResult.add(extra);
return toListResult;
}
@ -712,6 +754,8 @@ public class Api {
pigeonResult.setType(type == null ? null : NotificationEventType.values()[(int) type]);
Object payload = list.get(2);
pigeonResult.setPayload((String) payload);
Object extra = list.get(3);
pigeonResult.setExtra((Map<String, String>) extra);
return pigeonResult;
}
}

View File

@ -148,11 +148,19 @@ class NotificationReceiver : BroadcastReceiver() {
}
private fun handleTap(context: Context, intent: Intent) {
val extras = mutableMapOf<String?, String?>()
intent.extras?.keySet()!!.forEach {
if (it.startsWith("payload_")) {
extras[it.substring(8)] = intent.extras!!.getString(it)
}
}
MoxplatformAndroidPlugin.notificationSink?.success(
NotificationEvent().apply {
jid = intent.getStringExtra(NOTIFICATION_EXTRA_JID_KEY)!!
type = Api.NotificationEventType.OPEN
payload = null
extra = extras
}.toList()
)

View File

@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.Person
@ -117,6 +118,10 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
action = TAP_ACTION
putExtra(NOTIFICATION_EXTRA_JID_KEY, notification.jid)
putExtra(NOTIFICATION_EXTRA_ID_KEY, notification.id)
notification.extra?.forEach {
putExtra("payload_${it.key}", it.value)
}
}
val tapPendingIntent = PendingIntent.getBroadcast(
context,

View File

@ -106,6 +106,7 @@ class MessagingNotification {
required this.channelId,
required this.jid,
required this.messages,
this.extra,
});
/// The title of the conversation.
@ -123,6 +124,9 @@ class MessagingNotification {
/// Messages to show.
List<NotificationMessage?> messages;
/// Additional data to include.
Map<String?, String?>? extra;
Object encode() {
return <Object?>[
title,
@ -130,6 +134,7 @@ class MessagingNotification {
channelId,
jid,
messages,
extra,
];
}
@ -141,6 +146,7 @@ class MessagingNotification {
channelId: result[2]! as String,
jid: result[3]! as String,
messages: (result[4] as List<Object?>?)!.cast<NotificationMessage?>(),
extra: (result[5] as Map<Object?, Object?>?)?.cast<String?, String?>(),
);
}
}
@ -196,6 +202,7 @@ class NotificationEvent {
required this.jid,
required this.type,
this.payload,
this.extra,
});
/// The JID the notification was for.
@ -209,11 +216,15 @@ class NotificationEvent {
/// Otherwise: undefined.
String? payload;
/// Extra data. Only set when type == NotificationType.reply.
Map<String?, String?>? extra;
Object encode() {
return <Object?>[
jid,
type.index,
payload,
extra,
];
}
@ -223,6 +234,7 @@ class NotificationEvent {
jid: result[0]! as String,
type: NotificationEventType.values[result[1]! as int],
payload: result[2] as String?,
extra: (result[3] as Map<Object?, Object?>?)?.cast<String?, String?>(),
);
}
}

View File

@ -54,7 +54,7 @@ class NotificationMessage {
}
class MessagingNotification {
const MessagingNotification(this.title, this.id, this.jid, this.messages, this.channelId);
const MessagingNotification(this.title, this.id, this.jid, this.messages, this.channelId, this.extra);
/// The title of the conversation.
final String title;
@ -70,6 +70,9 @@ class MessagingNotification {
/// Messages to show.
final List<NotificationMessage?> messages;
/// Additional data to include.
final Map<String?, String?>? extra;
}
enum NotificationIcon {
@ -120,6 +123,9 @@ class NotificationEvent {
/// - type == NotificationType.reply: The reply message text.
/// Otherwise: undefined.
final String? payload;
/// Extra data. Only set when type == NotificationType.reply.
final Map<String?, String?>? extra;
}
class NotificationI18nData {