瀏覽代碼

Register notification channels on app start-up (Oreo or later)

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
Dariusz Olszewski 3 年之前
父節點
當前提交
2f4aa35c53

+ 1 - 23
app/src/gplay/java/com/nextcloud/talk/services/firebase/MagicFirebaseMessagingService.kt

@@ -23,14 +23,12 @@ import android.annotation.SuppressLint
 import android.app.Notification
 import android.app.PendingIntent
 import android.content.Intent
-import android.media.AudioAttributes
 import android.net.Uri
 import android.os.Bundle
 import android.os.Handler
 import android.util.Base64
 import android.util.Log
 import androidx.core.app.NotificationCompat
-import androidx.core.app.NotificationManagerCompat
 import androidx.emoji.text.EmojiCompat
 import androidx.work.Data
 import androidx.work.OneTimeWorkRequest
@@ -55,7 +53,6 @@ import com.nextcloud.talk.utils.ApiUtils
 import com.nextcloud.talk.utils.NotificationUtils
 import com.nextcloud.talk.utils.NotificationUtils.cancelAllNotificationsForAccount
 import com.nextcloud.talk.utils.NotificationUtils.cancelExistingNotificationWithId
-import com.nextcloud.talk.utils.NotificationUtils.createNotificationChannel
 import com.nextcloud.talk.utils.NotificationUtils.getCallRingtoneUri
 import com.nextcloud.talk.utils.PushUtils
 import com.nextcloud.talk.utils.bundle.BundleKeys
@@ -200,27 +197,8 @@ class MagicFirebaseMessagingService : FirebaseMessagingService() {
                                 PendingIntent.FLAG_UPDATE_CURRENT
                             )
 
-                            val audioAttributesBuilder =
-                                AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
-                            audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST)
-
-                            val soundUri = getCallRingtoneUri(applicationContext!!, appPreferences)
+                            val soundUri = getCallRingtoneUri(applicationContext!!, appPreferences!!)
                             val notificationChannelId = NotificationUtils.NOTIFICATION_CHANNEL_CALLS_V4
-                            createNotificationChannel(
-                                applicationContext!!,
-                                notificationChannelId,
-                                applicationContext.resources
-                                    .getString(R.string.nc_notification_channel_calls),
-                                applicationContext.resources
-                                    .getString(R.string.nc_notification_channel_calls_description),
-                                true,
-                                NotificationManagerCompat.IMPORTANCE_HIGH,
-                                soundUri!!,
-                                audioAttributesBuilder.build(),
-                                null,
-                                false
-                            )
-
                             val uri = Uri.parse(signatureVerification!!.userEntity.baseUrl)
                             val baseUrl = uri.host
 

+ 3 - 0
app/src/main/java/com/nextcloud/talk/application/NextcloudTalkApplication.kt

@@ -58,6 +58,7 @@ import com.nextcloud.talk.jobs.SignalingSettingsWorker
 import com.nextcloud.talk.utils.ClosedInterfaceImpl
 import com.nextcloud.talk.utils.DeviceUtils
 import com.nextcloud.talk.utils.DisplayUtils
+import com.nextcloud.talk.utils.NotificationUtils
 import com.nextcloud.talk.utils.OkHttpNetworkFetcherWithCache
 import com.nextcloud.talk.utils.database.arbitrarystorage.ArbitraryStorageModule
 import com.nextcloud.talk.utils.database.user.UserModule
@@ -188,6 +189,8 @@ class NextcloudTalkApplication : MultiDexApplication(), LifecycleObserver {
         val emojiCompat = EmojiCompat.init(config)
 
         EmojiManager.install(GoogleCompatEmojiProvider(emojiCompat))
+
+        NotificationUtils.registerNotificationChannels(applicationContext, appPreferences)
     }
 
     override fun onTerminate() {

+ 0 - 26
app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java

@@ -21,7 +21,6 @@
 package com.nextcloud.talk.jobs;
 
 import android.app.Notification;
-import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
@@ -332,34 +331,9 @@ public class NotificationWorker extends Worker {
         notificationBuilder.setExtras(notificationInfo);
 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
-
-            /*NotificationUtils.createNotificationChannelGroup(context,
-                    Long.toString(crc32.getValue()),
-                    groupName);*/
-
             if (CHAT.equals(decryptedPushMessage.getType()) || ROOM.equals(decryptedPushMessage.getType())) {
-                AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder().setContentType
-                        (AudioAttributes.CONTENT_TYPE_SONIFICATION);
-                audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT);
-                Uri soundUri = NotificationUtils.INSTANCE.getMessageRingtoneUri(getApplicationContext(),
-                                                                                appPreferences);
-                NotificationUtils.INSTANCE.createNotificationChannel(context,
-                        NotificationUtils.INSTANCE.getNOTIFICATION_CHANNEL_MESSAGES_V3(), context.getResources()
-                                .getString(R.string.nc_notification_channel_messages), context.getResources()
-                                .getString(R.string.nc_notification_channel_messages), true,
-                        NotificationManager.IMPORTANCE_HIGH, soundUri, audioAttributesBuilder.build(), null, false);
-
                 notificationBuilder.setChannelId(NotificationUtils.INSTANCE.getNOTIFICATION_CHANNEL_MESSAGES_V3());
-            } else {
-                /*NotificationUtils.INSTANCE.createNotificationChannel(context,
-                        NotificationUtils.INSTANCE.getNOTIFICATION_CHANNEL_CALLS_V3(), context.getResources()
-                                .getString(R.string.nc_notification_channel_calls), context.getResources()
-                                .getString(R.string.nc_notification_channel_calls_description), true,
-                        NotificationManager.IMPORTANCE_HIGH);
-
-                notificationBuilder.setChannelId(NotificationUtils.INSTANCE.getNOTIFICATION_CHANNEL_CALLS_V3());*/
             }
-
         } else {
             // red color for the lights
             notificationBuilder.setLights(0xFFFF0000, 200, 200);

+ 76 - 12
app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.kt

@@ -30,6 +30,7 @@ import android.net.Uri
 import android.os.Build
 import android.service.notification.StatusBarNotification
 import android.text.TextUtils
+import androidx.core.app.NotificationManagerCompat
 import com.bluelinelabs.logansquare.LoganSquare
 import com.nextcloud.talk.BuildConfig
 import com.nextcloud.talk.R
@@ -54,7 +55,7 @@ object NotificationUtils {
         "android.resource://" + BuildConfig.APPLICATION_ID + "/raw/librem_by_feandesign_message"
 
     @TargetApi(Build.VERSION_CODES.O)
-    fun createNotificationChannel(
+    private fun createNotificationChannel(
         context: Context,
         channelId: String,
         channelName: String,
@@ -95,8 +96,66 @@ object NotificationUtils {
         }
     }
 
+    private fun createCallsNotificationChannel(
+        context: Context,
+        appPreferences: AppPreferences
+    ) {
+        val audioAttributes =
+            AudioAttributes.Builder()
+                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
+                .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST)
+                .build()
+        val soundUri = getCallRingtoneUri(context, appPreferences)
+
+        createNotificationChannel(
+            context,
+            NOTIFICATION_CHANNEL_CALLS_V4,
+            context.resources.getString(R.string.nc_notification_channel_calls),
+            context.resources.getString(R.string.nc_notification_channel_calls_description),
+            true,
+            NotificationManagerCompat.IMPORTANCE_HIGH,
+            soundUri,
+            audioAttributes,
+            null,
+            false
+        )
+    }
+
+    private fun createMessagesNotificationChannel(
+        context: Context,
+        appPreferences: AppPreferences
+    ) {
+        val audioAttributes =
+            AudioAttributes.Builder()
+                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
+                .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
+                .build()
+        val soundUri = getMessageRingtoneUri(context, appPreferences)
+
+        createNotificationChannel(
+            context,
+            NOTIFICATION_CHANNEL_MESSAGES_V3,
+            context.resources.getString(R.string.nc_notification_channel_messages),
+            context.resources.getString(R.string.nc_notification_channel_messages),
+            true,
+            NotificationManager.IMPORTANCE_HIGH,
+            soundUri,
+            audioAttributes,
+            null,
+            false
+        )
+    }
+
+    fun registerNotificationChannels(
+        context: Context,
+        appPreferences: AppPreferences
+    ) {
+        createCallsNotificationChannel(context, appPreferences)
+        createMessagesNotificationChannel(context, appPreferences)
+    }
+
     @TargetApi(Build.VERSION_CODES.O)
-    fun getNotificationChannel(
+    private fun getNotificationChannel(
         context: Context,
         channelId: String
     ): NotificationChannel? {
@@ -214,17 +273,22 @@ object NotificationUtils {
         ringtonePreferencesString: String?,
         defaultRingtoneUri: String,
         channelId: String
-    ): Uri? {
+    ): Uri {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             val channel = getNotificationChannel(context, channelId)
-            return channel!!.sound
-        } else if (TextUtils.isEmpty(ringtonePreferencesString)) {
+            if (channel != null) {
+                return channel.sound
+            }
+            // Notification channel will not be available when starting the application for the first time.
+            // Ringtone uris are required to register the notification channels -> get uri from preferences.
+        }
+        if (TextUtils.isEmpty(ringtonePreferencesString)) {
             return Uri.parse(defaultRingtoneUri)
         } else {
             try {
                 val ringtoneSettings =
                     LoganSquare.parse(ringtonePreferencesString, RingtoneSettings::class.java)
-                return ringtoneSettings.ringtoneUri
+                return ringtoneSettings.ringtoneUri!!
             } catch (exception: IOException) {
                 return Uri.parse(defaultRingtoneUri)
             }
@@ -233,21 +297,21 @@ object NotificationUtils {
 
     fun getCallRingtoneUri(
         context: Context,
-        appPreferences: AppPreferences?
-    ): Uri? {
+        appPreferences: AppPreferences
+    ): Uri {
         return getRingtoneUri(
             context,
-            appPreferences!!.callRingtoneUri, DEFAULT_CALL_RINGTONE_URI, NOTIFICATION_CHANNEL_CALLS_V4
+            appPreferences.callRingtoneUri, DEFAULT_CALL_RINGTONE_URI, NOTIFICATION_CHANNEL_CALLS_V4
         )
     }
 
     fun getMessageRingtoneUri(
         context: Context,
-        appPreferences: AppPreferences?
-    ): Uri? {
+        appPreferences: AppPreferences
+    ): Uri {
         return getRingtoneUri(
             context,
-            appPreferences!!.messageRingtoneUri, DEFAULT_MESSAGE_RINGTONE_URI, NOTIFICATION_CHANNEL_MESSAGES_V3
+            appPreferences.messageRingtoneUri, DEFAULT_MESSAGE_RINGTONE_URI, NOTIFICATION_CHANNEL_MESSAGES_V3
         )
     }
 }