9
9
10
10
package im.vector.app.features.notifications
11
11
12
+ import android.Manifest
12
13
import android.annotation.SuppressLint
13
14
import android.app.Notification
14
15
import android.app.NotificationChannel
15
16
import android.app.NotificationManager
16
17
import android.app.PendingIntent
17
18
import android.content.Context
18
19
import android.content.Intent
20
+ import android.content.pm.PackageManager
19
21
import android.graphics.Bitmap
20
22
import android.graphics.Canvas
21
23
import android.net.Uri
@@ -27,6 +29,7 @@ import androidx.annotation.AttrRes
27
29
import androidx.annotation.ChecksSdkIntAtLeast
28
30
import androidx.annotation.DrawableRes
29
31
import androidx.annotation.StringRes
32
+ import androidx.core.app.ActivityCompat
30
33
import androidx.core.app.NotificationCompat
31
34
import androidx.core.app.NotificationManagerCompat
32
35
import androidx.core.app.RemoteInput
@@ -153,55 +156,59 @@ class NotificationUtils @Inject constructor(
153
156
* Default notification importance: shows everywhere, makes noise, but does not visually
154
157
* intrude.
155
158
*/
156
- notificationManager.createNotificationChannel(NotificationChannel (
157
- NOISY_NOTIFICATION_CHANNEL_ID ,
158
- stringProvider.getString(CommonStrings .notification_noisy_notifications).ifEmpty { " Noisy notifications" },
159
- NotificationManager .IMPORTANCE_DEFAULT
160
- )
161
- .apply {
162
- description = stringProvider.getString(CommonStrings .notification_noisy_notifications)
163
- enableVibration(true )
164
- enableLights(true )
165
- lightColor = accentColor
166
- })
159
+ notificationManager.createNotificationChannel(
160
+ NotificationChannel (
161
+ NOISY_NOTIFICATION_CHANNEL_ID ,
162
+ stringProvider.getString(CommonStrings .notification_noisy_notifications).ifEmpty { " Noisy notifications" },
163
+ NotificationManager .IMPORTANCE_DEFAULT
164
+ )
165
+ .apply {
166
+ description = stringProvider.getString(CommonStrings .notification_noisy_notifications)
167
+ enableVibration(true )
168
+ enableLights(true )
169
+ lightColor = accentColor
170
+ })
167
171
168
172
/* *
169
173
* Low notification importance: shows everywhere, but is not intrusive.
170
174
*/
171
- notificationManager.createNotificationChannel(NotificationChannel (
172
- SILENT_NOTIFICATION_CHANNEL_ID ,
173
- stringProvider.getString(CommonStrings .notification_silent_notifications).ifEmpty { " Silent notifications" },
174
- NotificationManager .IMPORTANCE_LOW
175
- )
176
- .apply {
177
- description = stringProvider.getString(CommonStrings .notification_silent_notifications)
178
- setSound(null , null )
179
- enableLights(true )
180
- lightColor = accentColor
181
- })
182
-
183
- notificationManager.createNotificationChannel(NotificationChannel (
184
- LISTENING_FOR_EVENTS_NOTIFICATION_CHANNEL_ID ,
185
- stringProvider.getString(CommonStrings .notification_listening_for_events).ifEmpty { " Listening for events" },
186
- NotificationManager .IMPORTANCE_MIN
187
- )
188
- .apply {
189
- description = stringProvider.getString(CommonStrings .notification_listening_for_events)
190
- setSound(null , null )
191
- setShowBadge(false )
192
- })
193
-
194
- notificationManager.createNotificationChannel(NotificationChannel (
195
- CALL_NOTIFICATION_CHANNEL_ID ,
196
- stringProvider.getString(CommonStrings .call).ifEmpty { " Call" },
197
- NotificationManager .IMPORTANCE_HIGH
198
- )
199
- .apply {
200
- description = stringProvider.getString(CommonStrings .call)
201
- setSound(null , null )
202
- enableLights(true )
203
- lightColor = accentColor
204
- })
175
+ notificationManager.createNotificationChannel(
176
+ NotificationChannel (
177
+ SILENT_NOTIFICATION_CHANNEL_ID ,
178
+ stringProvider.getString(CommonStrings .notification_silent_notifications).ifEmpty { " Silent notifications" },
179
+ NotificationManager .IMPORTANCE_LOW
180
+ )
181
+ .apply {
182
+ description = stringProvider.getString(CommonStrings .notification_silent_notifications)
183
+ setSound(null , null )
184
+ enableLights(true )
185
+ lightColor = accentColor
186
+ })
187
+
188
+ notificationManager.createNotificationChannel(
189
+ NotificationChannel (
190
+ LISTENING_FOR_EVENTS_NOTIFICATION_CHANNEL_ID ,
191
+ stringProvider.getString(CommonStrings .notification_listening_for_events).ifEmpty { " Listening for events" },
192
+ NotificationManager .IMPORTANCE_MIN
193
+ )
194
+ .apply {
195
+ description = stringProvider.getString(CommonStrings .notification_listening_for_events)
196
+ setSound(null , null )
197
+ setShowBadge(false )
198
+ })
199
+
200
+ notificationManager.createNotificationChannel(
201
+ NotificationChannel (
202
+ CALL_NOTIFICATION_CHANNEL_ID ,
203
+ stringProvider.getString(CommonStrings .call).ifEmpty { " Call" },
204
+ NotificationManager .IMPORTANCE_HIGH
205
+ )
206
+ .apply {
207
+ description = stringProvider.getString(CommonStrings .call)
208
+ setSound(null , null )
209
+ enableLights(true )
210
+ lightColor = accentColor
211
+ })
205
212
}
206
213
207
214
fun getChannel (channelId : String ): NotificationChannel ? {
@@ -997,7 +1004,11 @@ class NotificationUtils @Inject constructor(
997
1004
}
998
1005
999
1006
fun showNotificationMessage (tag : String? , id : Int , notification : Notification ) {
1000
- notificationManager.notify(tag, id, notification)
1007
+ if (ActivityCompat .checkSelfPermission(context, Manifest .permission.POST_NOTIFICATIONS ) != PackageManager .PERMISSION_GRANTED ) {
1008
+ Timber .w(" Not allowed to notify." )
1009
+ } else {
1010
+ notificationManager.notify(tag, id, notification)
1011
+ }
1001
1012
}
1002
1013
1003
1014
fun cancelNotificationMessage (tag : String? , id : Int ) {
@@ -1025,30 +1036,34 @@ class NotificationUtils @Inject constructor(
1025
1036
1026
1037
@SuppressLint(" LaunchActivityFromNotification" )
1027
1038
fun displayDiagnosticNotification () {
1028
- val testActionIntent = Intent (context, TestNotificationReceiver ::class .java)
1029
- testActionIntent.action = actionIds.diagnostic
1030
- val testPendingIntent = PendingIntent .getBroadcast(
1031
- context,
1032
- 0 ,
1033
- testActionIntent,
1034
- PendingIntent .FLAG_UPDATE_CURRENT or PendingIntentCompat .FLAG_IMMUTABLE
1035
- )
1039
+ if (ActivityCompat .checkSelfPermission(context, Manifest .permission.POST_NOTIFICATIONS ) != PackageManager .PERMISSION_GRANTED ) {
1040
+ Timber .w(" Not allowed to notify." )
1041
+ } else {
1042
+ val testActionIntent = Intent (context, TestNotificationReceiver ::class .java)
1043
+ testActionIntent.action = actionIds.diagnostic
1044
+ val testPendingIntent = PendingIntent .getBroadcast(
1045
+ context,
1046
+ 0 ,
1047
+ testActionIntent,
1048
+ PendingIntent .FLAG_UPDATE_CURRENT or PendingIntentCompat .FLAG_IMMUTABLE
1049
+ )
1036
1050
1037
- notificationManager.notify(
1038
- " DIAGNOSTIC" ,
1039
- 888 ,
1040
- NotificationCompat .Builder (context, NOISY_NOTIFICATION_CHANNEL_ID )
1041
- .setContentTitle(buildMeta.applicationName)
1042
- .setContentText(stringProvider.getString(CommonStrings .settings_troubleshoot_test_push_notification_content))
1043
- .setSmallIcon(R .drawable.ic_notification)
1044
- .setLargeIcon(getBitmap(context, im.vector.lib.ui.styles.R .drawable.element_logo_green))
1045
- .setColor(ContextCompat .getColor(context, im.vector.lib.ui.styles.R .color.notification_accent_color))
1046
- .setPriority(NotificationCompat .PRIORITY_MAX )
1047
- .setCategory(NotificationCompat .CATEGORY_STATUS )
1048
- .setAutoCancel(true )
1049
- .setContentIntent(testPendingIntent)
1050
- .build()
1051
- )
1051
+ notificationManager.notify(
1052
+ " DIAGNOSTIC" ,
1053
+ 888 ,
1054
+ NotificationCompat .Builder (context, NOISY_NOTIFICATION_CHANNEL_ID )
1055
+ .setContentTitle(buildMeta.applicationName)
1056
+ .setContentText(stringProvider.getString(CommonStrings .settings_troubleshoot_test_push_notification_content))
1057
+ .setSmallIcon(R .drawable.ic_notification)
1058
+ .setLargeIcon(getBitmap(context, im.vector.lib.ui.styles.R .drawable.element_logo_green))
1059
+ .setColor(ContextCompat .getColor(context, im.vector.lib.ui.styles.R .color.notification_accent_color))
1060
+ .setPriority(NotificationCompat .PRIORITY_MAX )
1061
+ .setCategory(NotificationCompat .CATEGORY_STATUS )
1062
+ .setAutoCancel(true )
1063
+ .setContentIntent(testPendingIntent)
1064
+ .build()
1065
+ )
1066
+ }
1052
1067
}
1053
1068
1054
1069
private fun getBitmap (context : Context , @DrawableRes drawableRes : Int ): Bitmap ? {
0 commit comments