-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Push] Show notification on push notification (until sync is started) (…
…#1043) * Added sync pending notification Signed-off-by: Arnau Mora Gras <[email protected]> * Moved notify function to PushNotificationManager Signed-off-by: Arnau Mora Gras <[email protected]> * Added ongoing and only-alert-once Signed-off-by: Arnau Mora Gras <[email protected]> * Added notification hiding Signed-off-by: Arnau Mora Gras <[email protected]> * Got rid of `cancel` Signed-off-by: Arnau Mora Gras <[email protected]> * Fixed comments Signed-off-by: Arnau Mora Gras <[email protected]> * Added content intent and sub text Signed-off-by: Arnau Mora Gras <[email protected]> * Updated usages Signed-off-by: Arnau Mora Gras <[email protected]> * Review changes Signed-off-by: Arnau Mora Gras <[email protected]> --------- Signed-off-by: Arnau Mora Gras <[email protected]>
- Loading branch information
1 parent
26a670c
commit c805e54
Showing
6 changed files
with
96 additions
and
13 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
app/src/main/kotlin/at/bitfire/davdroid/push/PushNotificationManager.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,65 @@ | ||
package at.bitfire.davdroid.push | ||
|
||
import android.accounts.Account | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import at.bitfire.davdroid.R | ||
import at.bitfire.davdroid.ui.NotificationRegistry | ||
import at.bitfire.davdroid.ui.account.AccountActivity | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
|
||
class PushNotificationManager @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
private val notificationRegistry: NotificationRegistry | ||
) { | ||
|
||
/** | ||
* Generates the notification ID for a push notification. | ||
*/ | ||
private fun notificationId(account: Account, authority: String): Int { | ||
return account.name.hashCode() + account.type.hashCode() + authority.hashCode() | ||
} | ||
|
||
/** | ||
* Sends a notification to inform the user that a push notification has been received, the | ||
* sync has been scheduled, but it still has not run. | ||
*/ | ||
fun notify(account: Account, authority: String) { | ||
notificationRegistry.notifyIfPossible(notificationId(account, authority)) { | ||
NotificationCompat.Builder(context, notificationRegistry.CHANNEL_STATUS) | ||
.setSmallIcon(R.drawable.ic_sync) | ||
.setContentTitle(context.getString(R.string.sync_notification_pending_push_title)) | ||
.setContentText(context.getString(R.string.sync_notification_pending_push_message)) | ||
.setSubText(account.name) | ||
.setPriority(NotificationCompat.PRIORITY_LOW) | ||
.setCategory(NotificationCompat.CATEGORY_STATUS) | ||
.setAutoCancel(true) | ||
.setOnlyAlertOnce(true) | ||
.setContentIntent( | ||
PendingIntent.getActivity( | ||
context, | ||
0, | ||
Intent(context, AccountActivity::class.java).apply { | ||
putExtra(AccountActivity.EXTRA_ACCOUNT, account) | ||
}, | ||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT | ||
) | ||
) | ||
.build() | ||
} | ||
} | ||
|
||
/** | ||
* Once the sync has been started, the notification is no longer needed and can be dismissed. | ||
* It's safe to call this method even if the notification has not been shown. | ||
*/ | ||
fun dismiss(account: Account, authority: String) { | ||
NotificationManagerCompat.from(context) | ||
.cancel(notificationId(account, authority)) | ||
} | ||
|
||
} |
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