From 97389d45b516262ac4af6a7a1484176cf06200f8 Mon Sep 17 00:00:00 2001 From: Andrew Gunnerson Date: Fri, 24 May 2024 20:04:12 -0400 Subject: [PATCH] Use separate channels and IDs for foreground notifications and alerts This allows users to individually disable the types of notifications they don't want. Using separate notification IDs also prevents Android from dismissing the wrong notification when the service leaves foreground mode. Signed-off-by: Andrew Gunnerson --- .../com/github/tmo1/sms_ie/ExportWorker.kt | 8 ++-- .../com/github/tmo1/sms_ie/MainActivity.kt | 38 ++++++++++++++----- app/src/main/res/values-b+de/strings.xml | 2 - app/src/main/res/values-b+fr/strings.xml | 2 - app/src/main/res/values-es/strings.xml | 2 - app/src/main/res/values-fa/strings.xml | 2 - app/src/main/res/values-it/strings.xml | 2 - app/src/main/res/values-iw/strings.xml | 2 - app/src/main/res/values-nb-rNO/strings.xml | 2 - app/src/main/res/values-pl/strings.xml | 2 - app/src/main/res/values-pt/strings.xml | 2 - app/src/main/res/values-tr/strings.xml | 2 - app/src/main/res/values-vi/strings.xml | 2 - app/src/main/res/values-zh-rCN/strings.xml | 2 - app/src/main/res/values/strings.xml | 6 ++- 15 files changed, 37 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/github/tmo1/sms_ie/ExportWorker.kt b/app/src/main/java/com/github/tmo1/sms_ie/ExportWorker.kt index 90c0d4d..4525075 100644 --- a/app/src/main/java/com/github/tmo1/sms_ie/ExportWorker.kt +++ b/app/src/main/java/com/github/tmo1/sms_ie/ExportWorker.kt @@ -68,7 +68,7 @@ class ExportWorker(appContext: Context, workerParams: WorkerParameters) : // many binder transactions in the background, which can happen when exporting many // messages. Running the service in the foreground prevents the app from being killed. // https://android.googlesource.com/platform/frameworks/base.git/+/71d75c09b9a06732a6edb4d1488d2aa3eb779e14%5E%21/ - val foregroundNotification = NotificationCompat.Builder(applicationContext, CHANNEL_ID) + val foregroundNotification = NotificationCompat.Builder(applicationContext, CHANNEL_ID_PERSISTENT) .setSmallIcon(R.mipmap.ic_launcher_foreground) .setContentTitle(context.getString(R.string.scheduled_export_executing)) .setOngoing(true).build() @@ -78,7 +78,7 @@ class ExportWorker(appContext: Context, workerParams: WorkerParameters) : 0 } try { - setForeground(ForegroundInfo(0, foregroundNotification, foregroundFlags)) + setForeground(ForegroundInfo(NOTIFICATION_ID_PERSISTENT, foregroundNotification, foregroundFlags)) } catch (e: Exception) { // If the user didn't allow the disabling of battery optimizations, then Android 12+'s // restrictions for starting a foreground service from the background will prevent this @@ -156,7 +156,7 @@ class ExportWorker(appContext: Context, workerParams: WorkerParameters) : contacts ) else context.getString(R.string.scheduled_export_failure) // https://developer.android.com/training/notify-user/build-notification#builder - val builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID) + val builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID_ALERTS) //.setSmallIcon(R.drawable.ic_launcher_foreground) .setSmallIcon(R.drawable.ic_scheduled_export_done) .setContentTitle(context.getString(R.string.scheduled_export_executed)) @@ -164,7 +164,7 @@ class ExportWorker(appContext: Context, workerParams: WorkerParameters) : // https://developer.android.com/training/notify-user/build-notification#notify with(NotificationManagerCompat.from(applicationContext)) { // notificationId is a unique int for each notification that you must define - { notify(0, builder.build()) } + { notify(NOTIFICATION_ID_ALERT, builder.build()) } } } } diff --git a/app/src/main/java/com/github/tmo1/sms_ie/MainActivity.kt b/app/src/main/java/com/github/tmo1/sms_ie/MainActivity.kt index 2e58766..18e7bd3 100644 --- a/app/src/main/java/com/github/tmo1/sms_ie/MainActivity.kt +++ b/app/src/main/java/com/github/tmo1/sms_ie/MainActivity.kt @@ -67,7 +67,10 @@ const val IMPORT_CONTACTS = 6 const val BECOME_DEFAULT_SMS_APP = 100 const val PERMISSIONS_REQUEST = 1 const val LOG_TAG = "SMSIE" -const val CHANNEL_ID = "MYCHANNEL" +const val CHANNEL_ID_PERSISTENT = "PERSISTENT" +const val CHANNEL_ID_ALERTS = "ALERTS" +const val NOTIFICATION_ID_PERSISTENT = 0 +const val NOTIFICATION_ID_ALERT = 1 // PduHeaders are referenced here https://developer.android.com/reference/android/provider/Telephony.Mms.Addr#TYPE // and defined here https://android.googlesource.com/platform/frameworks/opt/mms/+/4bfcd8501f09763c10255442c2b48fad0c796baa/src/java/com/google/android/mms/pdu/PduHeaders.java @@ -172,16 +175,33 @@ class MainActivity : AppCompatActivity(), ConfirmWipeFragment.NoticeDialogListen // https://developer.android.com/training/notify-user/channels // https://developer.android.com/training/notify-user/build-notification#Priority if (SDK_INT >= Build.VERSION_CODES.O) { - // Create the NotificationChannel - val name = getString(R.string.channel_name) - val descriptionText = getString(R.string.channel_description) - val importance = NotificationManager.IMPORTANCE_DEFAULT - val mChannel = NotificationChannel(CHANNEL_ID, name, importance) - mChannel.description = descriptionText + val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager + // Register the channel with the system; you can't change the importance // or other notification behaviors after this - val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager - notificationManager.createNotificationChannel(mChannel) + + notificationManager.createNotificationChannel( + NotificationChannel( + CHANNEL_ID_PERSISTENT, + getString(R.string.persistent_channel_name), + NotificationManager.IMPORTANCE_DEFAULT + ).apply { + description = getString(R.string.persistent_channel_description) + } + ) + + notificationManager.createNotificationChannel( + NotificationChannel( + CHANNEL_ID_ALERTS, + getString(R.string.alerts_channel_name), + NotificationManager.IMPORTANCE_DEFAULT + ).apply { + description = getString(R.string.alerts_channel_description) + } + ) + + // Remove legacy notification channels to accommodate upgrades + notificationManager.deleteNotificationChannel("MYCHANNEL") } } diff --git a/app/src/main/res/values-b+de/strings.xml b/app/src/main/res/values-b+de/strings.xml index 68fb395..4c90b7f 100644 --- a/app/src/main/res/values-b+de/strings.xml +++ b/app/src/main/res/values-b+de/strings.xml @@ -48,8 +48,6 @@ Die Datei scheint kein gültiges JSON-Array zu enthalten. Einstellungen Einschließlich binärer MMS-Daten - Benachrichtigungskanal - Benachrichtigungskanal Geplanter Export ausgeführt %1$d SMS, %2$d MMS , %3$d Anruf(e) und %4$d Kontakt(e) exportiert. Export fehlgeschlagen — siehe Logcat für weitere Details. diff --git a/app/src/main/res/values-b+fr/strings.xml b/app/src/main/res/values-b+fr/strings.xml index 56729e0..8cd4c54 100644 --- a/app/src/main/res/values-b+fr/strings.xml +++ b/app/src/main/res/values-b+fr/strings.xml @@ -44,8 +44,6 @@ L\'importation de l\'historique des appels nécessite l\'autorisation de lire et écrire l\'historique des appels. L\'exportation des contacts nécessite l\'autorisation de lire les contacts. L\'importation des contacts nécessite l\'autorisation d\'écrire les contacts. - canal_de_notification - Canal de Notification Exportation planifiée exécutée %1$d SMS(s), %2$d MMS(s), %3$d appel(s), et %4$d contact(s) exporté(s). Effacer les messages diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index a48ee19..0a24e4e 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -21,7 +21,6 @@ Para exportar los contactos se requiere el permiso de lectura de los contactos. Para importar los contactos se requiere permiso para escribir en los contactos. El archivo no parece contener un JSONArray válido. - Canal de notificaciones Exportación programada ejecutada %1$d SMS(es), %2$d MMS(es), %3$d llamadas y %4$d contactos exportados. Borrar mensajes @@ -49,7 +48,6 @@ Para importar los registros de llamadas se requiere permisos para leer y escribir en los registros de llamadas. Ajustes Incluir los datos binarios de los MMS - canal_de_notificaciones Exportación incorrecta — consulte el logcat para obtener más detalles. Advertencia: ¿Borrar todos los mensajes de este dispositivo de forma permanente, sin que se pueda deshacerlo\? Borrando los mensajes MMS … diff --git a/app/src/main/res/values-fa/strings.xml b/app/src/main/res/values-fa/strings.xml index da55ffd..a1e272b 100644 --- a/app/src/main/res/values-fa/strings.xml +++ b/app/src/main/res/values-fa/strings.xml @@ -24,8 +24,6 @@ درون‌برد گزارش‌های تماس برای خواندن و نوشتن گزارش‌های تماس به مجوز نیاز دارد. برون‌برد مخاطبین برای خواندن مخاطبین به مجوز نیاز دارد. تنظیمات - اطلاع‌رسانی_کانال - کانال اطلاع‌رسانی برون‌برد برنامه ریزی شده اجرا شد %1$d پیامک ، %2$d پیام چندرسانه‌ای، %3$d تماس و %4$d مخاطب برون‌برد شد. برون‌برد ناموفق بود — برای جزئیات بیشتر به گزارش مراجعه کنید. diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index e83629d..1afe115 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -60,8 +60,6 @@ Segnalazione problemi: Informazioni Esporta contatti - notification_channel - Canale notifiche Esecuzione esportazione pianificata Copia dati binari MMS ... Importazione messaggi... diff --git a/app/src/main/res/values-iw/strings.xml b/app/src/main/res/values-iw/strings.xml index 29d7740..b4cb070 100644 --- a/app/src/main/res/values-iw/strings.xml +++ b/app/src/main/res/values-iw/strings.xml @@ -16,9 +16,7 @@ ייצוא יומני שיחות דורש הרשאה לקרוא יומני שיחות ואנשי קשר. ייבוא יומני שיחות דורש הרשאה לקרוא ולכתוב יומני שיחות. כלול נתוני MMS בינאריים (מקודדים) - notification Channel ייצוא מתוזמן בוצע - Notification_channel %1$d הודעות סמס, %2$d mms, %3$d שיחות ו-%4$d אנשי קשר יוצאו. הייצוא לא הצליח - ראה logcat לפרטים נוספים. אזהרה: למחוק את כל ההודעות מהמכשיר הזה לצמיתות, מבלי שתוכל לבטל זאת\? diff --git a/app/src/main/res/values-nb-rNO/strings.xml b/app/src/main/res/values-nb-rNO/strings.xml index e8a60f5..e0c9812 100644 --- a/app/src/main/res/values-nb-rNO/strings.xml +++ b/app/src/main/res/values-nb-rNO/strings.xml @@ -15,7 +15,6 @@ Eksportering av anropslogger krever tilgang til å lese anropslogg og kontaktliste. Eksportering av meldinger krever tilgang til å lese SMS-er og kontaktliste. Programmet må være forvalgt meldingsprogram for å importere eller slette meldinger. - Merknadskanal %1$d SMS(-er), %2$d MMS(-er), og %3$d anrop eksportert. Tøm meldinger Hjemmeside: @@ -43,7 +42,6 @@ Innstillinger Planlagt eksport kjørt Inkluder (kodet) binær MMS-data - Merknadskanal Avbryt Filen ser ikke ut til å være et gyldig JSON-array. Tøm diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 821dac1..78a86d0 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -41,7 +41,6 @@ Wydaje się, że plik nie zawiera poprawnej tablicy JSONArray. Ustawienia Dołącz binarne dane MMS - Kanał powiadomień Zaplanowany eksport wykonany Wyeksportowano %1$d SMSów, %2$d MMSów, %3$d połączeń i %4$d kontaktów. Usuń wiadomości @@ -60,7 +59,6 @@ \nPrawa autorskie © 2021–24 \n \nWydane na licencji GNU GPLv3 - kanał_powiadomień Wyeksportowano %1$d z %2$d połączeń Kopiowanie binarnych danych MMS... Importowanie wiadomości... diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 54a89b2..e87df05 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -34,13 +34,11 @@ A apagar mensagens MMS … Documentação: Homepage: - canal_de_notificação %1$d chamada(s) importadas Reportar de erros: Importar / Exportar SMS Sobre %1$d de %2$d chamada(s) exportadas - Canal de Notificação Exportação sem sucesso — ver logcat para mais detalhes. Exportação agendada executada SMS Import / Export diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 2498eea..f532ca0 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -60,8 +60,6 @@ Dosya geçerli bir JSONArray içermiyor gibi görünüyor. Ayarlar İkili MMS verilerini dahil et - bildirim_kanali - Bildirim Kanalı Planlanan dışa aktarma gerçekleştirildi %1$d SMS, %2$d MMS, %3$d arama ve %4$d kisi dışa aktarıldı. Dışa aktarma başarısız — daha fazla ayrıntı için logcat\'e bakın. diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index 96284dc..6c5b507 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -53,12 +53,10 @@ Ứng dụng SMS mặc định Chỉ có thể nhập tin nhắn trên Android Marshmallow / 6.0 (API cấp 23) trở lên. Bao gồm dữ liệu MMS nhị phân - Kênh thông báo Cảnh báo: Xóa vĩnh viễn tất cả tin nhắn khỏi thiết bị này mà không thể hoàn tác? Xóa Hủy Đã hủy xóa - kênh_thông báo Việc xuất nhật ký cuộc gọi cần có quyền đọc Nhật ký cuộc gọi và Danh bạ. Thực hiện xuất khẩu theo lịch trình Đã thực hiện xuất theo lịch trình diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 4d69e86..42b94ac 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -50,11 +50,9 @@ 导入通话记录的功能,需要读取、写入通话记录的权限。 在文件中没找到有效的 JSONArray。 警告:要无法撤销地永久擦除此设备上的所有消息吗? - 通知通道 导出了 %1$d 位联系人 \n耗费时间:%2$s。 导出联系人需要读取联系人权限。 - notification_channel 导出联系人 导入联系人 导出了 %2$d 位联系人中的 %1$d 位 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 80b68ce..54d7b36 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -57,8 +57,10 @@ The file does not seem to contain a valid JSONArray. Settings Include binary MMS data - notification_channel - Notification Channel + Background services + Persistent notification for background scheduled exports + Scheduled export alerts + Success/failure alerts when scheduled exports complete Scheduled export executing Scheduled export executed %1$d SMS(s), %2$d MMS(s), %3$d calls, and %4$d contacts exported.