feat: Color in the notification silhouette

This commit is contained in:
PapaTutuWawa 2023-07-29 12:34:40 +02:00
parent daf40aed0b
commit 8f93821617
11 changed files with 107 additions and 30 deletions

View File

@ -45,6 +45,8 @@ class MyAppState extends State<MyApp> {
"Test notification channel",
channelId,
false,
);
await MoxplatformPlugin.notifications.setI18n(
NotificationI18nData(
reply: "答える",
markAsRead: "読みた",
@ -128,9 +130,8 @@ class MyHomePage extends StatelessWidget {
title: const Text('Moxplatform Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
child: ListView(
children: [
ElevatedButton(
onPressed: _cryptoTest,
child: const Text('Test cryptography'),

View File

@ -703,12 +703,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, @NonNull NotificationI18nData i18n);
void createNotificationChannel(@NonNull String title, @NonNull String id, @NonNull Boolean urgent);
void showMessagingNotification(@NonNull MessagingNotification notification);
void setNotificationSelfAvatar(@NonNull String path);
void setNotificationI18n(@NonNull NotificationI18nData data);
@NonNull
String getPersistentDataPath();
@ -735,9 +737,8 @@ public class Api {
String titleArg = (String) args.get(0);
String idArg = (String) args.get(1);
Boolean urgentArg = (Boolean) args.get(2);
NotificationI18nData i18nArg = (NotificationI18nData) args.get(3);
try {
api.createNotificationChannel(titleArg, idArg, urgentArg, i18nArg);
api.createNotificationChannel(titleArg, idArg, urgentArg);
wrapped.add(0, null);
}
catch (Throwable exception) {
@ -788,6 +789,30 @@ public class Api {
api.setNotificationSelfAvatar(pathArg);
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.setNotificationI18n", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
NotificationI18nData dataArg = (NotificationI18nData) args.get(0);
try {
api.setNotificationI18n(dataArg);
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;

View File

@ -288,7 +288,7 @@ import kotlin.jvm.functions.Function1;
}
@Override
public void createNotificationChannel(@NonNull String title, @NonNull String id, @NonNull Boolean urgent, @NonNull NotificationI18nData i18n) {
public void createNotificationChannel(@NonNull String title, @NonNull String id, @NonNull Boolean urgent) {
final NotificationChannel channel = new NotificationChannel(
id,
title,
@ -298,11 +298,6 @@ import kotlin.jvm.functions.Function1;
channel.enableLights(true);
final NotificationManager manager = getSystemService(context, NotificationManager.class);
manager.createNotificationChannel(channel);
// Configure i18n
NotificationDataManager.INSTANCE.setYou(i18n.getYou());
NotificationDataManager.INSTANCE.setReply(i18n.getReply());
NotificationDataManager.INSTANCE.setMarkAsRead(i18n.getMarkAsRead());
}
@Override
@ -315,6 +310,14 @@ import kotlin.jvm.functions.Function1;
NotificationDataManager.INSTANCE.setAvatarPath(path);
}
@Override
public void setNotificationI18n(@NonNull NotificationI18nData data) {
// Configure i18n
NotificationDataManager.INSTANCE.setYou(data.getYou());
NotificationDataManager.INSTANCE.setReply(data.getReply());
NotificationDataManager.INSTANCE.setMarkAsRead(data.getMarkAsRead());
}
@NonNull
@Override
public String getPersistentDataPath() {

View File

@ -1,9 +1,11 @@
package me.polynom.moxplatform_android
import android.app.Notification
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.Person
@ -38,8 +40,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
PendingIntent.FLAG_UPDATE_CURRENT,
)
val replyAction = NotificationCompat.Action.Builder(
// TODO: Wrong icon?
R.drawable.ic_service_icon,
R.drawable.reply,
NotificationDataManager.reply,
replyPendingIntent,
).apply {
@ -60,8 +61,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
PendingIntent.FLAG_UPDATE_CURRENT,
)
val markAsReadAction = NotificationCompat.Action.Builder(
// TODO: Wrong icon
R.drawable.ic_service_icon,
R.drawable.mark_as_read,
NotificationDataManager.markAsRead,
markAsReadPendingIntent,
).build()
@ -141,8 +141,11 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
// Assemble the notification
val finalNotification = NotificationCompat.Builder(context, notification.channelId).apply {
setStyle(style)
// TODO: I think this is wrong
// NOTE: It's okay to use the service icon here as I cannot get Android to display the
// actual logo. So we'll have to make do with the silhouette and the color purple.
setSmallIcon(R.drawable.ic_service_icon)
color = Color.argb(255, 207, 74, 255)
setColorized(true)
// Tap action
setContentIntent(tapPendingIntent)
@ -151,6 +154,9 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
addAction(replyAction)
addAction(markAsReadAction)
setAllowSystemGeneratedContextualActions(true)
setCategory(Notification.CATEGORY_MESSAGE)
// Prevent no notification when we replied before
setOnlyAlertOnce(false)
}.build()

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M17.34,20l-3.54,-3.54l1.41,-1.41l2.12,2.12l4.24,-4.24L23,14.34L17.34,20zM12,17c0,-3.87 3.13,-7 7,-7c1.08,0 2.09,0.25 3,0.68V4c0,-1.1 -0.9,-2 -2,-2H4C2.9,2 2,2.9 2,4v18l4,-4h6v0c0,-0.17 0.01,-0.33 0.03,-0.5C12.01,17.34 12,17.17 12,17z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10,9V5l-7,7 7,7v-4.1c5,0 8.5,1.6 11,5.1 -1,-5 -4,-10 -11,-11z"/>
</vector>

View File

@ -13,9 +13,8 @@ class AndroidNotificationsImplementation extends NotificationsImplementation {
String title,
String id,
bool urgent,
NotificationI18nData i18n,
) async {
return _api.createNotificationChannel(title, id, urgent, i18n);
return _api.createNotificationChannel(title, id, urgent);
}
@override
@ -28,7 +27,12 @@ class AndroidNotificationsImplementation extends NotificationsImplementation {
@override
Future<void> setNotificationSelfAvatar(String path) async {
return _api.setNotificationSelfAvatar(path);
}
}
@override
Future<void> setI18n(NotificationI18nData data) {
return _api.setNotificationI18n(data);
}
@override
Stream<NotificationEvent> getEventStream() => _channel

View File

@ -262,12 +262,12 @@ class MoxplatformApi {
static const MessageCodec<Object?> codec = _MoxplatformApiCodec();
Future<void> createNotificationChannel(String arg_title, String arg_id, bool arg_urgent, NotificationI18nData arg_i18n) async {
Future<void> createNotificationChannel(String arg_title, String arg_id, bool arg_urgent) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.createNotificationChannel', codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_title, arg_id, arg_urgent, arg_i18n]) as List<Object?>?;
await channel.send(<Object?>[arg_title, arg_id, arg_urgent]) as List<Object?>?;
if (replyList == null) {
throw PlatformException(
code: 'channel-error',
@ -328,6 +328,28 @@ class MoxplatformApi {
}
}
Future<void> setNotificationI18n(NotificationI18nData arg_data) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.setNotificationI18n', codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_data]) as List<Object?>?;
if (replyList == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyList.length > 1) {
throw PlatformException(
code: replyList[0]! as String,
message: replyList[1] as String?,
details: replyList[2],
);
} else {
return;
}
}
Future<String> getPersistentDataPath() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.getPersistentDataPath', codec,

View File

@ -2,11 +2,18 @@ import 'dart:async';
import 'package:moxplatform_platform_interface/src/api.g.dart';
abstract class NotificationsImplementation {
Future<void> createNotificationChannel(String title, String id, bool urgent, NotificationI18nData i18n);
/// Creates a notification channel with the name [title] and id [id]. If [urgent] is true, then
/// it configures the channel as carrying urgent information.
Future<void> createNotificationChannel(String title, String id, bool urgent);
/// Shows a notification [notification] in the messaging style with everyting it needs.
Future<void> showMessagingNotification(MessagingNotification notification);
/// Sets the path to the self-avatar for in-notification replies.
Future<void> setNotificationSelfAvatar(String path);
/// Configures the i18n data for usage in notifications.
Future<void> setI18n(NotificationI18nData data);
Stream<NotificationEvent> getEventStream();
}

View File

@ -4,7 +4,7 @@ import 'package:moxplatform_platform_interface/src/notifications.dart';
class StubNotificationsImplementation extends NotificationsImplementation {
@override
Future<void> createNotificationChannel(String title, String id, bool urgent, NotificationI18nData i18n) async {}
Future<void> createNotificationChannel(String title, String id, bool urgent) async {}
@override
Future<void> showMessagingNotification(MessagingNotification notification) async {}
@ -12,6 +12,9 @@ class StubNotificationsImplementation extends NotificationsImplementation {
@override
Future<void> setNotificationSelfAvatar(String path) async {}
@override
Future<void> setI18n(NotificationI18nData data) async {}
@override
Stream<NotificationEvent> getEventStream() {
return StreamController<NotificationEvent>().stream;

View File

@ -112,15 +112,11 @@ class NotificationI18nData {
@HostApi()
abstract class MoxplatformApi {
void createNotificationChannel(String title, String id, bool urgent, NotificationI18nData i18n);
void createNotificationChannel(String title, String id, bool urgent);
void showMessagingNotification(MessagingNotification notification);
void setNotificationSelfAvatar(String path);
void setNotificationI18n(NotificationI18nData data);
String getPersistentDataPath();
String getCacheDataPath();
void eventStub(NotificationEvent event);
}