diff --git a/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt b/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt index 65b8609446..6236aad65c 100644 --- a/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt +++ b/vector/src/fdroid/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt @@ -22,17 +22,21 @@ import im.vector.app.fdroid.features.settings.troubleshoot.TestBatteryOptimizati import im.vector.app.features.settings.troubleshoot.NotificationTroubleshootTestManager import im.vector.app.features.settings.troubleshoot.TestAccountSettings import im.vector.app.features.settings.troubleshoot.TestDeviceSettings +import im.vector.app.features.settings.troubleshoot.TestNotification import im.vector.app.features.settings.troubleshoot.TestPushRulesSettings import im.vector.app.features.settings.troubleshoot.TestSystemSettings import javax.inject.Inject -class NotificationTroubleshootTestManagerFactory @Inject constructor(private val testSystemSettings: TestSystemSettings, - private val testAccountSettings: TestAccountSettings, - private val testDeviceSettings: TestDeviceSettings, - private val testPushRulesSettings: TestPushRulesSettings, - private val testAutoStartBoot: TestAutoStartBoot, - private val testBackgroundRestrictions: TestBackgroundRestrictions, - private val testBatteryOptimization: TestBatteryOptimization) { +class NotificationTroubleshootTestManagerFactory @Inject constructor( + private val testSystemSettings: TestSystemSettings, + private val testAccountSettings: TestAccountSettings, + private val testDeviceSettings: TestDeviceSettings, + private val testPushRulesSettings: TestPushRulesSettings, + private val testAutoStartBoot: TestAutoStartBoot, + private val testBackgroundRestrictions: TestBackgroundRestrictions, + private val testBatteryOptimization: TestBatteryOptimization, + private val testNotification: TestNotification +) { fun create(fragment: Fragment): NotificationTroubleshootTestManager { val mgr = NotificationTroubleshootTestManager(fragment) @@ -43,6 +47,7 @@ class NotificationTroubleshootTestManagerFactory @Inject constructor(private val mgr.addTest(testAutoStartBoot) mgr.addTest(testBackgroundRestrictions) mgr.addTest(testBatteryOptimization) + mgr.addTest(testNotification) return mgr } } diff --git a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt index ce94244ee6..da93d54075 100644 --- a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt +++ b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt @@ -56,15 +56,15 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat } override fun onSuccess(data: Unit) { - // Wait for user to click on the notification - description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_success) - status = TestStatus.WAITING_FOR_USER + // Wait for the push to be received + description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push) + status = TestStatus.RUNNING } }) } - override fun onNotificationClicked() { - description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_notification_clicked) + override fun onPushReceived() { + description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_success) status = TestStatus.SUCCESS } diff --git a/vector/src/gplay/java/im/vector/app/gplay/push/fcm/VectorFirebaseMessagingService.kt b/vector/src/gplay/java/im/vector/app/gplay/push/fcm/VectorFirebaseMessagingService.kt index 801b85ce0c..cfd241d4f9 100755 --- a/vector/src/gplay/java/im/vector/app/gplay/push/fcm/VectorFirebaseMessagingService.kt +++ b/vector/src/gplay/java/im/vector/app/gplay/push/fcm/VectorFirebaseMessagingService.kt @@ -19,10 +19,12 @@ package im.vector.app.gplay.push.fcm +import android.content.Intent import android.os.Handler import android.os.Looper import androidx.lifecycle.Lifecycle import androidx.lifecycle.ProcessLifecycleOwner +import androidx.localbroadcastmanager.content.LocalBroadcastManager import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage import im.vector.app.BuildConfig @@ -34,6 +36,7 @@ import im.vector.app.features.badge.BadgeProxy import im.vector.app.features.notifications.NotifiableEventResolver import im.vector.app.features.notifications.NotifiableMessageEvent import im.vector.app.features.notifications.NotificationDrawerManager +import im.vector.app.features.notifications.NotificationUtils import im.vector.app.features.notifications.SimpleNotifiableEvent import im.vector.app.features.settings.VectorPreferences import im.vector.app.push.fcm.FcmHelper @@ -77,8 +80,8 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() { override fun onMessageReceived(message: RemoteMessage) { // Diagnostic Push if (message.data["event_id"] == PushersManager.TEST_EVENT_ID) { - // Display the notification right now - notificationDrawerManager.displayDiagnosticNotification() + val intent = Intent(NotificationUtils.PUSH_ACTION) + LocalBroadcastManager.getInstance(this).sendBroadcast(intent) return } diff --git a/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt b/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt index 46c78a9872..e96c603e60 100644 --- a/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt +++ b/vector/src/gplay/java/im/vector/app/push/fcm/NotificationTroubleshootTestManagerFactory.kt @@ -19,6 +19,7 @@ import androidx.fragment.app.Fragment import im.vector.app.features.settings.troubleshoot.NotificationTroubleshootTestManager import im.vector.app.features.settings.troubleshoot.TestAccountSettings import im.vector.app.features.settings.troubleshoot.TestDeviceSettings +import im.vector.app.features.settings.troubleshoot.TestNotification import im.vector.app.features.settings.troubleshoot.TestPushRulesSettings import im.vector.app.features.settings.troubleshoot.TestSystemSettings import im.vector.app.gplay.features.settings.troubleshoot.TestFirebaseToken @@ -35,7 +36,8 @@ class NotificationTroubleshootTestManagerFactory @Inject constructor( private val testPlayServices: TestPlayServices, private val testFirebaseToken: TestFirebaseToken, private val testTokenRegistration: TestTokenRegistration, - private val testPushFromPushGateway: TestPushFromPushGateway + private val testPushFromPushGateway: TestPushFromPushGateway, + private val testNotification: TestNotification ) { fun create(fragment: Fragment): NotificationTroubleshootTestManager { @@ -48,6 +50,7 @@ class NotificationTroubleshootTestManagerFactory @Inject constructor( mgr.addTest(testFirebaseToken) mgr.addTest(testTokenRegistration) mgr.addTest(testPushFromPushGateway) + mgr.addTest(testNotification) return mgr } } diff --git a/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt b/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt index 5e722ae209..2b8df67012 100644 --- a/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt +++ b/vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt @@ -97,15 +97,15 @@ fun copyToClipboard(context: Context, text: CharSequence, showToast: Boolean = t * Shows notification settings for the current app. * In android O will directly opens the notification settings, in lower version it will show the App settings */ -fun startNotificationSettingsIntent(activity: AppCompatActivity, activityResultLauncher: ActivityResultLauncher) { +fun startNotificationSettingsIntent(context: Context, activityResultLauncher: ActivityResultLauncher) { val intent = Intent() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS - intent.putExtra(Settings.EXTRA_APP_PACKAGE, activity.packageName) + intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName) } else { intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS - intent.putExtra("app_package", activity.packageName) - intent.putExtra("app_uid", activity.applicationInfo?.uid) + intent.putExtra("app_package", context.packageName) + intent.putExtra("app_uid", context.applicationInfo?.uid) } activityResultLauncher.launch(intent) } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt index a30d4302f2..44eb278c64 100755 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt @@ -92,6 +92,7 @@ class NotificationUtils @Inject constructor(private val context: Context, const val DISMISS_ROOM_NOTIF_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.DISMISS_ROOM_NOTIF_ACTION" private const val TAP_TO_VIEW_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.TAP_TO_VIEW_ACTION" const val DIAGNOSTIC_ACTION = "${BuildConfig.APPLICATION_ID}.NotificationActions.DIAGNOSTIC" + const val PUSH_ACTION = "${BuildConfig.APPLICATION_ID}.PUSH" /* ========================================================================================== * IDs for channels diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt index 4843c1446a..9dc6dc1751 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt @@ -166,7 +166,11 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor( tryOrNull("Unable to register the receiver") { LocalBroadcastManager.getInstance(requireContext()) - .registerReceiver(broadcastReceiver, IntentFilter(NotificationUtils.DIAGNOSTIC_ACTION)) + .registerReceiver(broadcastReceiverPush, IntentFilter(NotificationUtils.PUSH_ACTION)) + } + tryOrNull("Unable to register the receiver") { + LocalBroadcastManager.getInstance(requireContext()) + .registerReceiver(broadcastReceiverNotification, IntentFilter(NotificationUtils.DIAGNOSTIC_ACTION)) } } @@ -174,11 +178,21 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor( super.onPause() tryOrNull { LocalBroadcastManager.getInstance(requireContext()) - .unregisterReceiver(broadcastReceiver) + .unregisterReceiver(broadcastReceiverPush) + } + tryOrNull { + LocalBroadcastManager.getInstance(requireContext()) + .unregisterReceiver(broadcastReceiverNotification) } } - private val broadcastReceiver = object : BroadcastReceiver() { + private val broadcastReceiverPush = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + testManager?.onDiagnosticPushReceived() + } + } + + private val broadcastReceiverNotification = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { testManager?.onDiagnosticNotificationClicked() } diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt index 566cb6d846..7e7ca57243 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt @@ -106,6 +106,10 @@ class NotificationTroubleshootTestManager(val fragment: Fragment) { testList.forEach { it.cancel() } } + fun onDiagnosticPushReceived() { + testList.forEach { it.onPushReceived() } + } + fun onDiagnosticNotificationClicked() { testList.forEach { it.onNotificationClicked() } } diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestNotification.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestNotification.kt new file mode 100644 index 0000000000..6f25ecfe39 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestNotification.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2018 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.app.features.settings.troubleshoot + +import android.content.Context +import android.content.Intent +import androidx.activity.result.ActivityResultLauncher +import im.vector.app.R +import im.vector.app.core.resources.StringProvider +import im.vector.app.core.utils.startNotificationSettingsIntent +import im.vector.app.features.notifications.NotificationUtils +import javax.inject.Inject + +/** + * Checks if notifications can be displayed and clicked by the user + */ +class TestNotification @Inject constructor(private val context: Context, + private val notificationUtils: NotificationUtils, + private val stringProvider: StringProvider) + : TroubleshootTest(R.string.settings_troubleshoot_test_notification_title) { + + override fun perform(activityResultLauncher: ActivityResultLauncher) { + // Display the notification right now + notificationUtils.displayDiagnosticNotification() + description = stringProvider.getString(R.string.settings_troubleshoot_test_notification_notice) + + quickFix = object : TroubleshootQuickFix(R.string.open_settings) { + override fun doFix() { + startNotificationSettingsIntent(context, activityResultLauncher) + } + } + + status = TestStatus.WAITING_FOR_USER + } + + override fun onNotificationClicked() { + description = stringProvider.getString(R.string.settings_troubleshoot_test_notification_notification_clicked) + quickFix = null + status = TestStatus.SUCCESS + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestSystemSettings.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestSystemSettings.kt index 8e64514e7f..ee652288be 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestSystemSettings.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestSystemSettings.kt @@ -15,9 +15,9 @@ */ package im.vector.app.features.settings.troubleshoot +import android.content.Context import android.content.Intent import androidx.activity.result.ActivityResultLauncher -import androidx.appcompat.app.AppCompatActivity import androidx.core.app.NotificationManagerCompat import im.vector.app.R import im.vector.app.core.resources.StringProvider @@ -27,7 +27,7 @@ import javax.inject.Inject /** * Checks if notifications are enable in the system settings for this app. */ -class TestSystemSettings @Inject constructor(private val context: AppCompatActivity, +class TestSystemSettings @Inject constructor(private val context: Context, private val stringProvider: StringProvider) : TroubleshootTest(R.string.settings_troubleshoot_test_system_settings_title) { diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TroubleshootTest.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TroubleshootTest.kt index c775c8c827..76ba2378a0 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TroubleshootTest.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TroubleshootTest.kt @@ -53,6 +53,9 @@ abstract class TroubleshootTest(@StringRes val titleResId: Int) { open fun cancel() { } + open fun onPushReceived() { + } + open fun onNotificationClicked() { } } diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index ef6b63aeb0..e400f7fe64 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -750,10 +750,13 @@ Failed to register FCM token to HomeServer:\n%1$s Test Push - The application is receiving PUSH, please click on the test notification you just received. - The notification has been clicked! + The application is waiting for the PUSH + The application is receiving PUSH Failed to receive push. Solution could be to reinstall the application. - You are receiving PUSH! Click me! + You are viewing the notification! Click me! + Notification Display + Please click on the notification. If you do not see the notification, please check the system settings. + The notification has been clicked! Notifications Service Notifications Service is running.