Skip to content

Commit

Permalink
Use PendingIntentCompat to produce PendingIntent
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jun 17, 2024
1 parent 4e4fa72 commit 0c9c0ae
Showing 1 changed file with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.annotation.WorkerThread
import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.app.Person
import androidx.core.app.RemoteInput
import androidx.core.content.LocusIdCompat
Expand Down Expand Up @@ -143,18 +144,6 @@ class NotificationHelper @Inject constructor(@ApplicationContext context: Contex
)
}

private fun flagUpdateCurrent(mutable: Boolean): Int {
return if (mutable) {
if (Build.VERSION.SDK_INT >= 31) {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}
} else {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
}
}

@WorkerThread
fun showNotification(
contact: Contact,
Expand All @@ -166,15 +155,45 @@ class NotificationHelper @Inject constructor(@ApplicationContext context: Contex
val user = Person.Builder().setName(appContext.getString(R.string.sender_you)).build()
val person = Person.Builder().setName(contact.name).setIcon(icon).build()

val pendingIntent = PendingIntent.getActivity(
val pendingIntent = PendingIntentCompat.getActivity(
appContext,
REQUEST_BUBBLE,
// Launch BubbleActivity as the expanded bubble.
Intent(appContext, BubbleActivity::class.java)
.setAction(Intent.ACTION_VIEW)
.setData(contact.contentUri),
flagUpdateCurrent(mutable = true),
PendingIntent.FLAG_UPDATE_CURRENT,
true,
)
check(pendingIntent != null) {
"pendingIntent must not be null."
}

val contentPendingIntent = PendingIntentCompat.getActivity(
appContext,
REQUEST_CONTENT,
Intent(appContext, MainActivity::class.java)
.setAction(Intent.ACTION_VIEW)
.setData(contact.contentUri),
PendingIntent.FLAG_UPDATE_CURRENT,
false,
)
check(contentPendingIntent != null) {
"contentPendingIntent must not be null."
}

val actionPendingIntent = PendingIntentCompat.getBroadcast(
appContext,
REQUEST_CONTENT,
Intent(appContext, ReplyReceiver::class.java)
.setData(contact.contentUri),
PendingIntent.FLAG_UPDATE_CURRENT,
true,
)
check(actionPendingIntent != null) {
"actionPendingIntent must not be null."
}

// Let's add some more content to the notification in case it falls back to a normal
// notification.
val messagingStyle = NotificationCompat.MessagingStyle(user)
Expand Down Expand Up @@ -231,29 +250,14 @@ class NotificationHelper @Inject constructor(@ApplicationContext context: Contex
.setShowWhen(true)
// The content Intent is used when the user clicks on the "Open Content" icon button on
// the expanded bubble, as well as when the fall-back notification is clicked.
.setContentIntent(
PendingIntent.getActivity(
appContext,
REQUEST_CONTENT,
Intent(appContext, MainActivity::class.java)
.setAction(Intent.ACTION_VIEW)
.setData(contact.contentUri),
flagUpdateCurrent(mutable = false),
),
)
.setContentIntent(contentPendingIntent)
// Direct Reply
.addAction(
NotificationCompat.Action
.Builder(
IconCompat.createWithResource(appContext, R.drawable.ic_send),
appContext.getString(R.string.label_reply),
PendingIntent.getBroadcast(
appContext,
REQUEST_CONTENT,
Intent(appContext, ReplyReceiver::class.java)
.setData(contact.contentUri),
flagUpdateCurrent(mutable = true),
),
actionPendingIntent,
)
.addRemoteInput(
RemoteInput.Builder(ReplyReceiver.KEY_TEXT_REPLY)
Expand Down

0 comments on commit 0c9c0ae

Please sign in to comment.