-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send notifs when install ready if backgrounded
- Loading branch information
1 parent
21d4d57
commit 5ddd74b
Showing
9 changed files
with
158 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/src/main/kotlin/com/aliucord/manager/ui/util/InstallNotifications.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.aliucord.manager.ui.util | ||
|
||
import android.Manifest | ||
import android.app.* | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.util.Log | ||
import androidx.annotation.StringRes | ||
import androidx.core.app.* | ||
import androidx.core.content.ContextCompat | ||
import com.aliucord.manager.* | ||
import com.aliucord.manager.util.findActivity | ||
|
||
object InstallNotifications { | ||
private const val CHANNEL_ID = "installation" | ||
|
||
/** | ||
* Creates or replaces a notification with id [id] that brings | ||
* up the existing [MainActivity] when clicked upon. | ||
* | ||
* @param id A unique notification ID for different notifications | ||
* @param title Main notification title | ||
* @param description Notification description | ||
*/ | ||
fun createNotification( | ||
context: Context, | ||
id: Int, | ||
@StringRes title: Int, | ||
@StringRes description: Int, | ||
) { | ||
val manager = NotificationManagerCompat.from(context) | ||
|
||
// Create the target notification channel | ||
if (Build.VERSION.SDK_INT >= 26 && manager.getNotificationChannel(CHANNEL_ID) == null) { | ||
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH) | ||
.setName(context.getString(R.string.notif_group_install_title)) | ||
.setDescription(context.getString(R.string.notif_group_install_desc)) | ||
.build() | ||
|
||
manager.createNotificationChannel(channel) | ||
} | ||
|
||
val notification = NotificationCompat.Builder(context, CHANNEL_ID) | ||
.setDefaults(Notification.DEFAULT_LIGHTS or Notification.DEFAULT_SOUND) | ||
.setAutoCancel(true) | ||
.setSmallIcon(R.drawable.ic_aliucord_logo) | ||
.setContentTitle(context.getString(title)) | ||
.setContentText(context.getString(description)) | ||
.setContentIntent( | ||
PendingIntent.getActivity( | ||
/* context = */ context, | ||
/* requestCode = */ 0, | ||
/* intent = */ | ||
Intent(context, MainActivity::class.java) | ||
.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), | ||
/* flags = */ PendingIntent.FLAG_IMMUTABLE, | ||
) | ||
) | ||
.build() | ||
|
||
try { | ||
manager.notify(id, notification) | ||
} catch (e: SecurityException) { | ||
Log.w(BuildConfig.TAG, "Failed to send install notification", e) | ||
} | ||
} | ||
|
||
/** | ||
* Request the `POST_NOTIFICATIONS` permission if needed. | ||
*/ | ||
fun requestPermissions(context: Context) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return | ||
|
||
val granted = ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) | ||
val activity = context.findActivity() ?: return | ||
|
||
if (granted != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions( | ||
activity, | ||
arrayOf(Manifest.permission.POST_NOTIFICATIONS), | ||
0, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters