diff --git a/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushManager.kt b/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushManager.kt index 26da350372..c6f0161a43 100644 --- a/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushManager.kt +++ b/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushManager.kt @@ -25,6 +25,8 @@ class HuaweiPushManager @Inject constructor( @Synchronized override fun refresh(force: Boolean) { + Log.d(TAG, "refresh() called with: force = $force") + val huaweiPushInstanceIdJob = huaweiPushInstanceIdJob huaweiPushInstanceIdJob?.apply { @@ -35,9 +37,12 @@ class HuaweiPushManager @Inject constructor( val tokenScope = "HCM" val hmsInstanceId = HmsInstanceId.getInstance(context) - genericPushManager.refresh(TextSecurePreferences.getFCMToken(context), force) + Log.d(TAG, "hmsInstanceId: $hmsInstanceId") + +// genericPushManager.refresh(TextSecurePreferences.getFCMToken(context), force) MainScope().launch(Dispatchers.IO) { + Log.d(TAG, "hmInstanceId getting token...") val token = hmsInstanceId.getToken(appId, tokenScope) Log.d(TAG, "refresh() hmsInstanceId => huawei token: $token") // diff --git a/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushNotificationService.kt b/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushNotificationService.kt index 16ac969d2c..a0ce0283d4 100644 --- a/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushNotificationService.kt +++ b/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushNotificationService.kt @@ -5,52 +5,63 @@ import com.huawei.hms.push.HmsMessageService import com.huawei.hms.push.RemoteMessage import dagger.hilt.android.AndroidEntryPoint import org.session.libsession.utilities.TextSecurePreferences +import org.session.libsignal.utilities.Base64 import org.session.libsignal.utilities.Log import java.lang.Exception import javax.inject.Inject +private val TAG = HuaweiPushNotificationService::class.java.simpleName + @AndroidEntryPoint class HuaweiPushNotificationService: HmsMessageService() { init { - Log.d("pnh", "init Huawei Service") + Log.d(TAG, "init Huawei Service") } @Inject lateinit var pushManager: PushManager + @Inject lateinit var genericPushManager: GenericPushManager @Inject lateinit var pushHandler: PushHandler override fun onCreate() { - Log.d("pnh", "onCreate Huawei Service") + Log.d(TAG, "onCreate Huawei Service") super.onCreate() } - override fun onMessageDelivered(p0: String?, p1: Exception?) { - Log.d("pnh", "onMessageDelivered") - super.onMessageDelivered(p0, p1) + override fun onMessageReceived(message: RemoteMessage?) { + Log.d(TAG, "onMessageReceived: $message.") + pushHandler.onPush(message?.data?.let(Base64::decode)) } override fun onMessageSent(p0: String?) { - Log.d("pnh", "onMessageSent") + Log.d(TAG, "onMessageSent() called with: p0 = $p0") super.onMessageSent(p0) } + override fun onSendError(p0: String?, p1: Exception?) { + Log.d(TAG, "onSendError() called with: p0 = $p0, p1 = $p1") + super.onSendError(p0, p1) + } + + override fun onMessageDelivered(p0: String?, p1: Exception?) { + Log.d(TAG, "onMessageDelivered") + super.onMessageDelivered(p0, p1) + } + + override fun onNewToken(p0: String?) { - Log.d("pnh", "onNewToken") + Log.d(TAG, "onNewToken") super.onNewToken(p0) } override fun onNewToken(token: String?, bundle: Bundle?) { - Log.d("pnh", "New HCM token: $token.") - - if (token == TextSecurePreferences.getFCMToken(this)) return + Log.d(TAG, "New HCM token: $token.") TextSecurePreferences.setFCMToken(this, token) - pushManager.refresh(true) - } - override fun onMessageReceived(message: RemoteMessage?) { - Log.d("pnh", "onMessageReceived: $message.") - pushHandler.onPush(message?.dataOfMap) + + genericPushManager.refresh(token, true) } + override fun onDeletedMessages() { pushManager.refresh(true) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 2fe1b1e1c7..f3cf59255f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -506,10 +506,10 @@ public class ApplicationContext extends Application implements DefaultLifecycleO firebaseInstanceIdJob.cancel(null); } String displayName = TextSecurePreferences.getProfileName(this); - boolean isUsingFCM = TextSecurePreferences.isUsingFCM(this); + boolean isUsingFCM = TextSecurePreferences.isPushEnabled(this); TextSecurePreferences.clearAll(this); if (isMigratingToV2KeyPair) { - TextSecurePreferences.setIsUsingFCM(this, isUsingFCM); + TextSecurePreferences.setPushEnabled(this, isUsingFCM); TextSecurePreferences.setProfileName(this, displayName); } getSharedPreferences(PREFERENCES_NAME, 0).edit().clear().commit(); diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/FcmTokenManager.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/FcmTokenManager.kt index 295c8296fc..cfccb7f721 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/FcmTokenManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/FcmTokenManager.kt @@ -12,7 +12,7 @@ class FcmTokenManager @Inject constructor( ) { private val expiryManager = ExpiryManager(context) - val isUsingFCM get() = TextSecurePreferences.isUsingFCM(context) + val isUsingFCM get() = TextSecurePreferences.isPushEnabled(context) var fcmToken get() = TextSecurePreferences.getFCMToken(context) diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/PushHandler.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/PushHandler.kt index bba89d2e96..087d1ee982 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/PushHandler.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/PushHandler.kt @@ -33,7 +33,7 @@ class PushHandler @Inject constructor(@ApplicationContext val context: Context) onPush(dataMap?.asByteArray()) } - private fun onPush(data: ByteArray?) { + fun onPush(data: ByteArray?) { if (data == null) { onPush() return diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt index edaf646aaa..c6de3cd676 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt @@ -165,7 +165,7 @@ class PNModeActivity : BaseActionBarActivity() { return } - TextSecurePreferences.setIsUsingFCM(this, (selectedOptionView == binding.fcmOptionView)) + TextSecurePreferences.setPushEnabled(this, (selectedOptionView == binding.fcmOptionView)) val application = ApplicationContext.getInstance(this) application.startPollingIfNeeded() pushManager.refresh(true) diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java b/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java index 228392ad76..ea0510d579 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java @@ -45,10 +45,10 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme // Set up FCM toggle String fcmKey = "pref_key_use_fcm"; - ((SwitchPreferenceCompat)findPreference(fcmKey)).setChecked(TextSecurePreferences.isUsingFCM(getContext())); + ((SwitchPreferenceCompat)findPreference(fcmKey)).setChecked(TextSecurePreferences.isPushEnabled(getContext())); this.findPreference(fcmKey) .setOnPreferenceChangeListener((preference, newValue) -> { - TextSecurePreferences.setIsUsingFCM(getContext(), (boolean) newValue); + TextSecurePreferences.setPushEnabled(getContext(), (boolean) newValue); pushManager.refresh(true); return true; }); diff --git a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt index 074ef379f8..8bd19de61d 100644 --- a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt +++ b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt @@ -27,7 +27,9 @@ class FirebasePushManager @Inject constructor( firebaseInstanceIdJob = getFcmInstanceId { task -> when { - task.isSuccessful -> try { task.result?.token?.let { genericPushManager.refresh(it, force).get() } } catch(e: Exception) { Log.e(TAG, "refresh() failed", e) } + task.isSuccessful -> try { task.result?.token?.let { + genericPushManager.refresh(it, force).get() + } } catch(e: Exception) { Log.e(TAG, "refresh() failed", e) } else -> Log.w(TAG, "getFcmInstanceId failed." + task.exception) } } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt index 3e82bda4a3..d1a822ae3d 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt @@ -28,7 +28,7 @@ object PushManagerV1 { fun register( device: Device, - isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), + isUsingFCM: Boolean = TextSecurePreferences.isPushEnabled(context), token: String? = TextSecurePreferences.getFCMToken(context), publicKey: String? = TextSecurePreferences.getLocalNumber(context), legacyGroupPublicKeys: Collection = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys() @@ -94,7 +94,7 @@ object PushManagerV1 { sendOnionRequest(request) success { when (it.code) { - null, 0 -> throw Exception("error: ${it.message}.") + null, 0 -> Log.d(TAG, "error: ${it.message}.") else -> Log.d(TAG, "unregisterV1 success") } } @@ -105,7 +105,7 @@ object PushManagerV1 { fun subscribeGroup( closedGroupPublicKey: String, - isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), + isUsingFCM: Boolean = TextSecurePreferences.isPushEnabled(context), publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!! ) = if (isUsingFCM) { performGroupOperation("subscribe_closed_group", closedGroupPublicKey, publicKey) @@ -113,7 +113,7 @@ object PushManagerV1 { fun unsubscribeGroup( closedGroupPublicKey: String, - isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), + isUsingFCM: Boolean = TextSecurePreferences.isPushEnabled(context), publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!! ) = if (isUsingFCM) { performGroupOperation("unsubscribe_closed_group", closedGroupPublicKey, publicKey) diff --git a/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt b/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt index 349546838d..ee17fb2c26 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt @@ -37,12 +37,12 @@ interface TextSecurePreferences { fun setLastConfigurationSyncTime(value: Long) fun getConfigurationMessageSynced(): Boolean fun setConfigurationMessageSynced(value: Boolean) - fun isUsingFCM(): Boolean - fun setIsUsingFCM(value: Boolean) - fun getFCMToken(): String? - fun setFCMToken(value: String) - fun getLastFCMUploadTime(): Long - fun setLastFCMUploadTime(value: Long) + fun isPushEnabled(): Boolean + fun setPushEnabled(value: Boolean) + fun getPushToken(): String? + fun setPushToken(value: String) + fun getPushRegisterTime(): Long + fun setPushRegisterTime(value: Long) fun isScreenLockEnabled(): Boolean fun setScreenLockEnabled(value: Boolean) fun getScreenLockTimeout(): Long @@ -251,9 +251,9 @@ interface TextSecurePreferences { const val LINK_PREVIEWS = "pref_link_previews" const val GIF_METADATA_WARNING = "has_seen_gif_metadata_warning" const val GIF_GRID_LAYOUT = "pref_gif_grid_layout" - const val IS_USING_FCM = "pref_is_using_fcm" + const val IS_PUSH_ENABLED = "pref_is_using_fcm" const val FCM_TOKEN = "pref_fcm_token_2" - const val LAST_FCM_TOKEN_UPLOAD_TIME = "pref_last_fcm_token_upload_time_2" + const val PUSH_REGISTER_TIME = "pref_last_fcm_token_upload_time_2" const val LAST_CONFIGURATION_SYNC_TIME = "pref_last_configuration_sync_time" const val CONFIGURATION_SYNCED = "pref_configuration_synced" const val LAST_PROFILE_UPDATE_TIME = "pref_last_profile_update_time" @@ -309,13 +309,13 @@ interface TextSecurePreferences { } @JvmStatic - fun isUsingFCM(context: Context): Boolean { - return getBooleanPreference(context, IS_USING_FCM, false) + fun isPushEnabled(context: Context): Boolean { + return getBooleanPreference(context, IS_PUSH_ENABLED, false) } @JvmStatic - fun setIsUsingFCM(context: Context, value: Boolean) { - setBooleanPreference(context, IS_USING_FCM, value) + fun setPushEnabled(context: Context, value: Boolean) { + setBooleanPreference(context, IS_PUSH_ENABLED, value) } @JvmStatic @@ -329,11 +329,11 @@ interface TextSecurePreferences { } fun getLastFCMUploadTime(context: Context): Long { - return getLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, 0) + return getLongPreference(context, PUSH_REGISTER_TIME, 0) } fun setLastFCMUploadTime(context: Context, value: Long) { - setLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, value) + setLongPreference(context, PUSH_REGISTER_TIME, value) } // endregion @@ -1032,28 +1032,28 @@ class AppTextSecurePreferences @Inject constructor( TextSecurePreferences._events.tryEmit(TextSecurePreferences.CONFIGURATION_SYNCED) } - override fun isUsingFCM(): Boolean { - return getBooleanPreference(TextSecurePreferences.IS_USING_FCM, false) + override fun isPushEnabled(): Boolean { + return getBooleanPreference(TextSecurePreferences.IS_PUSH_ENABLED, false) } - override fun setIsUsingFCM(value: Boolean) { - setBooleanPreference(TextSecurePreferences.IS_USING_FCM, value) + override fun setPushEnabled(value: Boolean) { + setBooleanPreference(TextSecurePreferences.IS_PUSH_ENABLED, value) } - override fun getFCMToken(): String? { + override fun getPushToken(): String? { return getStringPreference(TextSecurePreferences.FCM_TOKEN, "") } - override fun setFCMToken(value: String) { + override fun setPushToken(value: String) { setStringPreference(TextSecurePreferences.FCM_TOKEN, value) } - override fun getLastFCMUploadTime(): Long { - return getLongPreference(TextSecurePreferences.LAST_FCM_TOKEN_UPLOAD_TIME, 0) + override fun getPushRegisterTime(): Long { + return getLongPreference(TextSecurePreferences.PUSH_REGISTER_TIME, 0) } - override fun setLastFCMUploadTime(value: Long) { - setLongPreference(TextSecurePreferences.LAST_FCM_TOKEN_UPLOAD_TIME, value) + override fun setPushRegisterTime(value: Long) { + setLongPreference(TextSecurePreferences.PUSH_REGISTER_TIME, value) } override fun isScreenLockEnabled(): Boolean {