From d1d3f23f826b8ca0fe5c79ca7ddc17f4afb43364 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 8 Feb 2022 12:35:25 -0500 Subject: [PATCH] android notification policy/content --- .../kotlin/im/cwtch/flwtch/FlwtchWorker.kt | 89 +++++++++++-------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt index c72b1969..d9575127 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt @@ -68,52 +68,69 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : val handle = if (evt.EventType == "NewMessageFromPeer") data.getString("RemotePeer") else data.getString("GroupID"); if (data["RemotePeer"] != data["ProfileOnion"]) { - val loader = FlutterInjector.instance().flutterLoader() - val key = loader.getLookupKeyForAsset("assets/" + data.getString("picture"))//"assets/profiles/001-centaur.png") - val fh = applicationContext.assets.open(key) + val notification = data["notification"] - if (notification != "None") { + + if (notification == "SimpleEvent") { + val channelId = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createMessageNotificationChannel("Cwtch", "Cwtch") + } else { + // If earlier version channel ID is not used + // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) + "" + } + val clickIntent = Intent(applicationContext, MainActivity::class.java).also { intent -> + intent.action = Intent.ACTION_RUN + intent.putExtra("EventType", "NotificationClicked") + } + + val newNotification = NotificationCompat.Builder(applicationContext, channelId) + .setContentTitle("Cwtch") + .setContentText("New message")//todo: translate + .setSmallIcon(R.mipmap.knott_transparent) + .setContentIntent(PendingIntent.getActivity(applicationContext, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)) + .setAutoCancel(true) + .build() + + notificationManager.notify(getNotificationID("Cwtch", "Cwtch"), newNotification) } else if (notification == "ContactInfo") { + val channelId = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createMessageNotificationChannel(handle, handle) + } else { + // If earlier version channel ID is not used + // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) + "" + } + val loader = FlutterInjector.instance().flutterLoader() + val key = loader.getLookupKeyForAsset("assets/" + data.getString("Picture"))//"assets/profiles/001-centaur.png") + val fh = applicationContext.assets.open(key) + val clickIntent = Intent(applicationContext, MainActivity::class.java).also { intent -> + intent.action = Intent.ACTION_RUN + intent.putExtra("EventType", "NotificationClicked") + intent.putExtra("ProfileOnion", data.getString("ProfileOnion")) + intent.putExtra("Handle", handle) + } + + val newNotification = NotificationCompat.Builder(applicationContext, channelId) + .setContentTitle(data.getString("Nick")) + .setContentText("New message")//todo: translate + .setLargeIcon(BitmapFactory.decodeStream(fh)) + .setSmallIcon(R.mipmap.knott_transparent) + .setContentIntent(PendingIntent.getActivity(applicationContext, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)) + .setAutoCancel(true) + .build() + + notificationManager.notify(getNotificationID(data.getString("ProfileOnion"), handle), newNotification) } - - val channelId = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - createMessageNotificationChannel(handle, handle) - } else { - // If earlier version channel ID is not used - // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) - "" - } - - val loader = FlutterInjector.instance().flutterLoader() - val key = loader.getLookupKeyForAsset("assets/" + data.getString("Picture"))//"assets/profiles/001-centaur.png") - val fh = applicationContext.assets.open(key) - - - val clickIntent = Intent(applicationContext, MainActivity::class.java).also { intent -> - intent.action = Intent.ACTION_RUN - intent.putExtra("EventType", "NotificationClicked") - intent.putExtra("ProfileOnion", data.getString("ProfileOnion")) - intent.putExtra("Handle", handle) - } - - val newNotification = NotificationCompat.Builder(applicationContext, channelId) - .setContentTitle(data.getString("Nick")) - .setContentText("New message")//todo: translate - .setLargeIcon(BitmapFactory.decodeStream(fh)) - .setSmallIcon(R.mipmap.knott_transparent) - .setContentIntent(PendingIntent.getActivity(applicationContext, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)) - .setAutoCancel(true) - .build() - notificationManager.notify(getNotificationID(data.getString("ProfileOnion"), handle), newNotification) - } } } else if (evt.EventType == "FileDownloadProgressUpdate") { try {