From 70453ef2d5555fb29b5f388eb9abff92a1f82b2c Mon Sep 17 00:00:00 2001 From: Grisha Kruglov Date: Tue, 28 May 2019 15:27:44 -0700 Subject: [PATCH] No issue: increase likelyhood of notification showing up as 'heads-up' --- .../fenix/components/BackgroundServices.kt | 3 +++ .../fenix/components/NotificationManager.kt | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt index 7276f8b6c..131f78626 100644 --- a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt +++ b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt @@ -20,6 +20,7 @@ import mozilla.components.feature.sync.GlobalSyncableStoreProvider import mozilla.components.service.fxa.Config import mozilla.components.service.fxa.manager.DeviceTuple import mozilla.components.service.fxa.manager.FxaAccountManager +import mozilla.components.support.base.log.logger.Logger import org.mozilla.fenix.R import org.mozilla.fenix.test.Mockable @@ -58,7 +59,9 @@ class BackgroundServices( } private val deviceEventObserver = object : DeviceEventsObserver { + private val logger = Logger("DeviceEventsObserver") override fun onEvents(events: List) { + logger.info("Received ${events.size} device event(s)") events.filter { it is DeviceEvent.TabReceived }.forEach { notificationManager.showReceivedTabs(it as DeviceEvent.TabReceived) } diff --git a/app/src/main/java/org/mozilla/fenix/components/NotificationManager.kt b/app/src/main/java/org/mozilla/fenix/components/NotificationManager.kt index 39e7b8295..06aa57742 100644 --- a/app/src/main/java/org/mozilla/fenix/components/NotificationManager.kt +++ b/app/src/main/java/org/mozilla/fenix/components/NotificationManager.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.components import android.annotation.TargetApi +import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent @@ -16,6 +17,7 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import mozilla.components.concept.sync.DeviceEvent import mozilla.components.concept.sync.TabData +import mozilla.components.support.base.log.logger.Logger import org.mozilla.fenix.R /** @@ -43,9 +45,12 @@ class NotificationManager(private val context: Context) { } } + private val logger = Logger("NotificationManager") + fun showReceivedTabs(event: DeviceEvent.TabReceived) { // In the future, experiment with displaying multiple tabs from the same device as as Notification Groups. // For now, a single notification per tab received will suffice. + logger.debug("Showing ${event.entries.size} tab(s) received from deviceID=${event.from?.id}") event.entries.forEach { tab -> val intent = Intent(Intent.ACTION_VIEW, Uri.parse(tab.url)) val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0) @@ -53,19 +58,27 @@ class NotificationManager(private val context: Context) { val builder = NotificationCompat.Builder(context, RECEIVE_TABS_CHANNEL_ID) .setSmallIcon(R.drawable.ic_status_logo) .setTitle(event, tab) + .setWhen(System.currentTimeMillis()) .setContentText(tab.url) .setContentIntent(pendingIntent) .setAutoCancel(true) // Explicitly set a priority for = Build.VERSION_CODES.M) { + builder.setCategory(Notification.CATEGORY_REMINDER) + } + + val notification = builder.build() // Pick a random ID for this notification so that different tabs do not clash. @SuppressWarnings("MagicNumber") val notificationId = (Math.random() * 100).toInt() with(NotificationManagerCompat.from(context)) { - notify(RECEIVE_TABS_TAG, notificationId, builder.build()) + notify(RECEIVE_TABS_TAG, notificationId, notification) } } }