fix: Add payload to all intents

This commit is contained in:
2023-07-30 20:40:59 +02:00
parent c7ee2b6c6e
commit 2490a8ee9f
10 changed files with 171 additions and 32 deletions

View File

@@ -387,6 +387,20 @@ public class Api {
this.messages = setterArg;
}
/** Flag indicating whether this notification is from a groupchat or not. */
private @NonNull Boolean isGroupchat;
public @NonNull Boolean getIsGroupchat() {
return isGroupchat;
}
public void setIsGroupchat(@NonNull Boolean setterArg) {
if (setterArg == null) {
throw new IllegalStateException("Nonnull field \"isGroupchat\" is null.");
}
this.isGroupchat = setterArg;
}
/** Additional data to include. */
private @Nullable Map<String, String> extra;
@@ -438,6 +452,13 @@ public class Api {
return this;
}
private @Nullable Boolean isGroupchat;
public @NonNull Builder setIsGroupchat(@NonNull Boolean setterArg) {
this.isGroupchat = setterArg;
return this;
}
private @Nullable Map<String, String> extra;
public @NonNull Builder setExtra(@Nullable Map<String, String> setterArg) {
@@ -452,6 +473,7 @@ public class Api {
pigeonReturn.setChannelId(channelId);
pigeonReturn.setJid(jid);
pigeonReturn.setMessages(messages);
pigeonReturn.setIsGroupchat(isGroupchat);
pigeonReturn.setExtra(extra);
return pigeonReturn;
}
@@ -459,12 +481,13 @@ public class Api {
@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(6);
ArrayList<Object> toListResult = new ArrayList<Object>(7);
toListResult.add(title);
toListResult.add(id);
toListResult.add(channelId);
toListResult.add(jid);
toListResult.add(messages);
toListResult.add(isGroupchat);
toListResult.add(extra);
return toListResult;
}
@@ -481,7 +504,9 @@ public class Api {
pigeonResult.setJid((String) jid);
Object messages = list.get(4);
pigeonResult.setMessages((List<NotificationMessage>) messages);
Object extra = list.get(5);
Object isGroupchat = list.get(5);
pigeonResult.setIsGroupchat((Boolean) isGroupchat);
Object extra = list.get(6);
pigeonResult.setExtra((Map<String, String>) extra);
return pigeonResult;
}
@@ -914,12 +939,14 @@ public class Api {
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
public interface MoxplatformApi {
void createNotificationChannel(@NonNull String title, @NonNull String id, @NonNull Boolean urgent);
void createNotificationChannel(@NonNull String title, @NonNull String description, @NonNull String id, @NonNull Boolean urgent);
void showMessagingNotification(@NonNull MessagingNotification notification);
void showNotification(@NonNull RegularNotification notification);
void dismissNotification(@NonNull Long id);
void setNotificationSelfAvatar(@NonNull String path);
void setNotificationI18n(@NonNull NotificationI18nData data);
@@ -948,10 +975,11 @@ public class Api {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String titleArg = (String) args.get(0);
String idArg = (String) args.get(1);
Boolean urgentArg = (Boolean) args.get(2);
String descriptionArg = (String) args.get(1);
String idArg = (String) args.get(2);
Boolean urgentArg = (Boolean) args.get(3);
try {
api.createNotificationChannel(titleArg, idArg, urgentArg);
api.createNotificationChannel(titleArg, descriptionArg, idArg, urgentArg);
wrapped.add(0, null);
}
catch (Throwable exception) {
@@ -1002,6 +1030,30 @@ public class Api {
api.showNotification(notificationArg);
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.dismissNotification", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
Number idArg = (Number) args.get(0);
try {
api.dismissNotification((idArg == null) ? null : idArg.longValue());
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;

View File

@@ -2,8 +2,8 @@ package me.polynom.moxplatform_android;
import static androidx.core.content.ContextCompat.getSystemService;
import static me.polynom.moxplatform_android.ConstantsKt.SHARED_PREFERENCES_KEY;
import static me.polynom.moxplatform_android.RecordSentMessageKt.recordSentMessage;
import static me.polynom.moxplatform_android.CryptoKt.*;
import static me.polynom.moxplatform_android.RecordSentMessageKt.*;
import me.polynom.moxplatform_android.Api.*;
@@ -18,6 +18,7 @@ import android.content.SharedPreferences;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@@ -39,7 +40,7 @@ import io.flutter.plugin.common.JSONMethodCodec;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
public class MoxplatformAndroidPlugin extends BroadcastReceiver implements FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler, ServiceAware, MoxplatformApi {
public class MoxplatformAndroidPlugin extends BroadcastReceiver implements FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler, ServiceAware, MoxplatformApi {
public static final String entrypointKey = "entrypoint_handle";
public static final String extraDataKey = "extra_data";
private static final String autoStartAtBootKey = "auto_start_at_boot";
@@ -258,10 +259,11 @@ public class MoxplatformAndroidPlugin extends BroadcastReceiver implements Flutt
}
@Override
public void createNotificationChannel(@NonNull String title, @NonNull String id, @NonNull Boolean urgent) {
public void createNotificationChannel(@NonNull String title, @NonNull String description, @NonNull String id, @NonNull Boolean urgent) {
final NotificationChannel channel = new NotificationChannel(id, title, urgent ? NotificationManager.IMPORTANCE_HIGH : NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(true);
channel.enableLights(true);
channel.setDescription(description);
final NotificationManager manager = getSystemService(context, NotificationManager.class);
manager.createNotificationChannel(channel);
}
@@ -276,6 +278,11 @@ public class MoxplatformAndroidPlugin extends BroadcastReceiver implements Flutt
NotificationsKt.showNotification(context, notification);
}
@Override
public void dismissNotification(@NonNull Long id) {
NotificationManagerCompat.from(context).cancel(id.intValue());
}
@Override
public void setNotificationSelfAvatar(@NonNull String path) {
NotificationDataManager.INSTANCE.setAvatarPath(path);

View File

@@ -42,16 +42,30 @@ class NotificationReceiver : BroadcastReceiver() {
.find { it.id == id }?.notification
}
private fun extractPayloadMapFromIntent(intent: Intent): Map<String?, String?> {
val extras = mutableMapOf<String?, String?>()
intent.extras?.keySet()!!.forEach {
Log.d(TAG, "Checking $it -> ${intent.extras!!.get(it)}")
if (it.startsWith("payload_")) {
Log.d(TAG, "Adding $it")
extras[it.substring(8)] = intent.extras!!.getString(it)
}
}
return extras
}
private fun handleMarkAsRead(context: Context, intent: Intent) {
NotificationManagerCompat.from(context).cancel(intent.getLongExtra(MARK_AS_READ_ID_KEY, -1).toInt())
MoxplatformAndroidPlugin.notificationSink?.success(
NotificationEvent().apply {
jid = intent.getStringExtra(NOTIFICATION_EXTRA_JID_KEY)!!
type = Api.NotificationEventType.MARK_AS_READ
payload = null
extra = extractPayloadMapFromIntent(intent)
}.toList()
)
NotificationManagerCompat.from(context).cancel(intent.getLongExtra(MARK_AS_READ_ID_KEY, -1).toInt())
dismissNotification(context, intent);
}
@@ -63,6 +77,7 @@ class NotificationReceiver : BroadcastReceiver() {
jid = intent.getStringExtra(NOTIFICATION_EXTRA_JID_KEY)!!
type = Api.NotificationEventType.REPLY
payload = replyPayload.toString()
extra = extractPayloadMapFromIntent(intent)
}.toList()
)
@@ -148,19 +163,12 @@ 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
extra = extractPayloadMapFromIntent(intent)
}.toList()
)

View File

@@ -14,6 +14,7 @@ import androidx.core.app.RemoteInput
import androidx.core.content.FileProvider
import androidx.core.graphics.drawable.IconCompat
import java.io.File
import java.lang.Exception
/*
* Holds "persistent" data for notifications, like i18n strings. While not useful now, this is
@@ -78,6 +79,10 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
action = REPLY_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 replyPendingIntent = PendingIntent.getBroadcast(
context.applicationContext,
@@ -99,6 +104,10 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
action = MARK_AS_READ_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 markAsReadPendingIntent = PendingIntent.getBroadcast(
context.applicationContext,
@@ -144,7 +153,14 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
}
}.build()
val style = NotificationCompat.MessagingStyle(selfPerson);
for (message in notification.messages) {
style.isGroupConversation = notification.isGroupchat
if (notification.isGroupchat) {
style.conversationTitle = notification.title
}
for (i in notification.messages.indices) {
val message = notification.messages[i]
// Build the sender
val sender = Person.Builder().apply {
setName(message.sender)
@@ -152,11 +168,15 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
// Set the avatar, if available
if (message.avatarPath != null) {
setIcon(
IconCompat.createWithAdaptiveBitmap(
BitmapFactory.decodeFile(message.avatarPath),
),
)
try {
setIcon(
IconCompat.createWithAdaptiveBitmap(
BitmapFactory.decodeFile(message.avatarPath),
),
)
} catch (ex: Throwable) {
Log.w(TAG, "Failed to open avatar at ${message.avatarPath}")
}
}
}.build()
@@ -204,6 +224,11 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
addAction(replyAction)
addAction(markAsReadAction)
// Groupchat title
if (notification.isGroupchat) {
setContentTitle(notification.title)
}
setAllowSystemGeneratedContextualActions(true)
setCategory(Notification.CATEGORY_MESSAGE)