11package org.thoughtcrime.securesms.notifications
22
3+ import android.Manifest
4+ import android.app.PendingIntent
35import android.content.Context
6+ import android.content.Intent
7+ import android.content.pm.PackageManager
8+ import androidx.core.app.ActivityCompat
9+ import androidx.core.app.NotificationCompat
10+ import androidx.core.app.NotificationManagerCompat
11+ import androidx.core.content.ContextCompat.getString
412import dagger.hilt.android.qualifiers.ApplicationContext
513import kotlinx.coroutines.CoroutineScope
614import kotlinx.coroutines.launch
715import kotlinx.serialization.json.Json
16+ import network.loki.messenger.R
817import network.loki.messenger.libsession_util.Namespace
918import network.loki.messenger.libsession_util.SessionEncrypt
1019import org.session.libsession.messaging.messages.Message.Companion.senderOrSync
@@ -26,6 +35,7 @@ import org.thoughtcrime.securesms.database.ReceivedMessageHashDatabase
2635import org.thoughtcrime.securesms.dependencies.ConfigFactory
2736import org.thoughtcrime.securesms.dependencies.ManagerScope
2837import org.thoughtcrime.securesms.groups.GroupRevokedMessageHandler
38+ import org.thoughtcrime.securesms.home.HomeActivity
2939import javax.inject.Inject
3040
3141private const val TAG = " PushHandler"
@@ -40,6 +50,8 @@ class PushReceiver @Inject constructor(
4050 private val receivedMessageHashDatabase : ReceivedMessageHashDatabase ,
4151 @param:ManagerScope private val scope : CoroutineScope ,
4252 private val loginStateRepository : LoginStateRepository ,
53+ private val notificationChannelManager : NotificationChannelManager ,
54+ private val notificationManagerCompat : NotificationManagerCompat ,
4355) {
4456
4557 /* *
@@ -79,6 +91,7 @@ class PushReceiver @Inject constructor(
7991
8092 // send a generic notification if we have no data
8193 if (pushData.data == null ) {
94+ sendGenericNotification()
8295 return
8396 }
8497
@@ -146,6 +159,10 @@ class PushReceiver @Inject constructor(
146159 namespace == Namespace .DEFAULT () || pushData?.metadata == null -> {
147160 if (pushData?.data == null ) {
148161 Log .d(TAG , " Push data is null" )
162+ if (pushData?.metadata?.data_too_long != true ) {
163+ Log .d(TAG , " Sending a generic notification (data_too_long was false)" )
164+ sendGenericNotification()
165+ }
149166 return
150167 }
151168
@@ -187,6 +204,33 @@ class PushReceiver @Inject constructor(
187204
188205 }
189206
207+
208+ private fun sendGenericNotification () {
209+ // no need to do anything if notification permissions are not granted
210+ if (ActivityCompat .checkSelfPermission(
211+ context,
212+ Manifest .permission.POST_NOTIFICATIONS
213+ ) != PackageManager .PERMISSION_GRANTED
214+ ) {
215+ return
216+ }
217+
218+ val builder = NotificationCompat .Builder (context,
219+ notificationChannelManager.getNotificationChannelId(NotificationChannelManager .ChannelDescription .ONE_TO_ONE_MESSAGES ))
220+ .setSmallIcon(R .drawable.ic_notification)
221+ .setColor(context.getColor(R .color.textsecure_primary))
222+ .setContentTitle(getString(context, R .string.app_name))
223+
224+ // Note: We set the count to 1 in the below plurals string so it says "You've got a new message" (singular)
225+ .setContentText(context.resources.getQuantityString(R .plurals.messageNewYouveGot, 1 , 1 ))
226+
227+ .setPriority(NotificationCompat .PRIORITY_DEFAULT )
228+ .setAutoCancel(true )
229+ .setContentIntent(PendingIntent .getActivity(context, 0 , Intent (context, HomeActivity ::class .java), PendingIntent .FLAG_IMMUTABLE ))
230+
231+ notificationManagerCompat.notify(NotificationId .LEGACY_PUSH , builder.build())
232+ }
233+
190234 private fun Map <String , String >.asPushData (): PushData =
191235 when {
192236 // this is a v2 push notification
0 commit comments