Fix Huawei push notifications

This commit is contained in:
andrew 2023-08-04 13:36:09 +09:30
parent cc6f880665
commit a27f81db30
10 changed files with 70 additions and 52 deletions

View File

@ -25,6 +25,8 @@ class HuaweiPushManager @Inject constructor(
@Synchronized @Synchronized
override fun refresh(force: Boolean) { override fun refresh(force: Boolean) {
Log.d(TAG, "refresh() called with: force = $force")
val huaweiPushInstanceIdJob = huaweiPushInstanceIdJob val huaweiPushInstanceIdJob = huaweiPushInstanceIdJob
huaweiPushInstanceIdJob?.apply { huaweiPushInstanceIdJob?.apply {
@ -35,9 +37,12 @@ class HuaweiPushManager @Inject constructor(
val tokenScope = "HCM" val tokenScope = "HCM"
val hmsInstanceId = HmsInstanceId.getInstance(context) 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) { MainScope().launch(Dispatchers.IO) {
Log.d(TAG, "hmInstanceId getting token...")
val token = hmsInstanceId.getToken(appId, tokenScope) val token = hmsInstanceId.getToken(appId, tokenScope)
Log.d(TAG, "refresh() hmsInstanceId => huawei token: $token") Log.d(TAG, "refresh() hmsInstanceId => huawei token: $token")
// //

View File

@ -5,52 +5,63 @@ import com.huawei.hms.push.HmsMessageService
import com.huawei.hms.push.RemoteMessage import com.huawei.hms.push.RemoteMessage
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsignal.utilities.Base64
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
import java.lang.Exception import java.lang.Exception
import javax.inject.Inject import javax.inject.Inject
private val TAG = HuaweiPushNotificationService::class.java.simpleName
@AndroidEntryPoint @AndroidEntryPoint
class HuaweiPushNotificationService: HmsMessageService() { class HuaweiPushNotificationService: HmsMessageService() {
init { init {
Log.d("pnh", "init Huawei Service") Log.d(TAG, "init Huawei Service")
} }
@Inject lateinit var pushManager: PushManager @Inject lateinit var pushManager: PushManager
@Inject lateinit var genericPushManager: GenericPushManager
@Inject lateinit var pushHandler: PushHandler @Inject lateinit var pushHandler: PushHandler
override fun onCreate() { override fun onCreate() {
Log.d("pnh", "onCreate Huawei Service") Log.d(TAG, "onCreate Huawei Service")
super.onCreate() super.onCreate()
} }
override fun onMessageDelivered(p0: String?, p1: Exception?) { override fun onMessageReceived(message: RemoteMessage?) {
Log.d("pnh", "onMessageDelivered") Log.d(TAG, "onMessageReceived: $message.")
super.onMessageDelivered(p0, p1) pushHandler.onPush(message?.data?.let(Base64::decode))
} }
override fun onMessageSent(p0: String?) { override fun onMessageSent(p0: String?) {
Log.d("pnh", "onMessageSent") Log.d(TAG, "onMessageSent() called with: p0 = $p0")
super.onMessageSent(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?) { override fun onNewToken(p0: String?) {
Log.d("pnh", "onNewToken") Log.d(TAG, "onNewToken")
super.onNewToken(p0) super.onNewToken(p0)
} }
override fun onNewToken(token: String?, bundle: Bundle?) { override fun onNewToken(token: String?, bundle: Bundle?) {
Log.d("pnh", "New HCM token: $token.") Log.d(TAG, "New HCM token: $token.")
if (token == TextSecurePreferences.getFCMToken(this)) return
TextSecurePreferences.setFCMToken(this, token) TextSecurePreferences.setFCMToken(this, token)
pushManager.refresh(true)
} genericPushManager.refresh(token, true)
override fun onMessageReceived(message: RemoteMessage?) {
Log.d("pnh", "onMessageReceived: $message.")
pushHandler.onPush(message?.dataOfMap)
} }
override fun onDeletedMessages() { override fun onDeletedMessages() {
pushManager.refresh(true) pushManager.refresh(true)
} }

View File

@ -506,10 +506,10 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
firebaseInstanceIdJob.cancel(null); firebaseInstanceIdJob.cancel(null);
} }
String displayName = TextSecurePreferences.getProfileName(this); String displayName = TextSecurePreferences.getProfileName(this);
boolean isUsingFCM = TextSecurePreferences.isUsingFCM(this); boolean isUsingFCM = TextSecurePreferences.isPushEnabled(this);
TextSecurePreferences.clearAll(this); TextSecurePreferences.clearAll(this);
if (isMigratingToV2KeyPair) { if (isMigratingToV2KeyPair) {
TextSecurePreferences.setIsUsingFCM(this, isUsingFCM); TextSecurePreferences.setPushEnabled(this, isUsingFCM);
TextSecurePreferences.setProfileName(this, displayName); TextSecurePreferences.setProfileName(this, displayName);
} }
getSharedPreferences(PREFERENCES_NAME, 0).edit().clear().commit(); getSharedPreferences(PREFERENCES_NAME, 0).edit().clear().commit();

View File

@ -12,7 +12,7 @@ class FcmTokenManager @Inject constructor(
) { ) {
private val expiryManager = ExpiryManager(context) private val expiryManager = ExpiryManager(context)
val isUsingFCM get() = TextSecurePreferences.isUsingFCM(context) val isUsingFCM get() = TextSecurePreferences.isPushEnabled(context)
var fcmToken var fcmToken
get() = TextSecurePreferences.getFCMToken(context) get() = TextSecurePreferences.getFCMToken(context)

View File

@ -33,7 +33,7 @@ class PushHandler @Inject constructor(@ApplicationContext val context: Context)
onPush(dataMap?.asByteArray()) onPush(dataMap?.asByteArray())
} }
private fun onPush(data: ByteArray?) { fun onPush(data: ByteArray?) {
if (data == null) { if (data == null) {
onPush() onPush()
return return

View File

@ -165,7 +165,7 @@ class PNModeActivity : BaseActionBarActivity() {
return return
} }
TextSecurePreferences.setIsUsingFCM(this, (selectedOptionView == binding.fcmOptionView)) TextSecurePreferences.setPushEnabled(this, (selectedOptionView == binding.fcmOptionView))
val application = ApplicationContext.getInstance(this) val application = ApplicationContext.getInstance(this)
application.startPollingIfNeeded() application.startPollingIfNeeded()
pushManager.refresh(true) pushManager.refresh(true)

View File

@ -45,10 +45,10 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
// Set up FCM toggle // Set up FCM toggle
String fcmKey = "pref_key_use_fcm"; String fcmKey = "pref_key_use_fcm";
((SwitchPreferenceCompat)findPreference(fcmKey)).setChecked(TextSecurePreferences.isUsingFCM(getContext())); ((SwitchPreferenceCompat)findPreference(fcmKey)).setChecked(TextSecurePreferences.isPushEnabled(getContext()));
this.findPreference(fcmKey) this.findPreference(fcmKey)
.setOnPreferenceChangeListener((preference, newValue) -> { .setOnPreferenceChangeListener((preference, newValue) -> {
TextSecurePreferences.setIsUsingFCM(getContext(), (boolean) newValue); TextSecurePreferences.setPushEnabled(getContext(), (boolean) newValue);
pushManager.refresh(true); pushManager.refresh(true);
return true; return true;
}); });

View File

@ -27,7 +27,9 @@ class FirebasePushManager @Inject constructor(
firebaseInstanceIdJob = getFcmInstanceId { task -> firebaseInstanceIdJob = getFcmInstanceId { task ->
when { 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) else -> Log.w(TAG, "getFcmInstanceId failed." + task.exception)
} }
} }

View File

@ -28,7 +28,7 @@ object PushManagerV1 {
fun register( fun register(
device: Device, device: Device,
isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), isUsingFCM: Boolean = TextSecurePreferences.isPushEnabled(context),
token: String? = TextSecurePreferences.getFCMToken(context), token: String? = TextSecurePreferences.getFCMToken(context),
publicKey: String? = TextSecurePreferences.getLocalNumber(context), publicKey: String? = TextSecurePreferences.getLocalNumber(context),
legacyGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys() legacyGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys()
@ -94,7 +94,7 @@ object PushManagerV1 {
sendOnionRequest(request) success { sendOnionRequest(request) success {
when (it.code) { when (it.code) {
null, 0 -> throw Exception("error: ${it.message}.") null, 0 -> Log.d(TAG, "error: ${it.message}.")
else -> Log.d(TAG, "unregisterV1 success") else -> Log.d(TAG, "unregisterV1 success")
} }
} }
@ -105,7 +105,7 @@ object PushManagerV1 {
fun subscribeGroup( fun subscribeGroup(
closedGroupPublicKey: String, closedGroupPublicKey: String,
isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), isUsingFCM: Boolean = TextSecurePreferences.isPushEnabled(context),
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!! publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
) = if (isUsingFCM) { ) = if (isUsingFCM) {
performGroupOperation("subscribe_closed_group", closedGroupPublicKey, publicKey) performGroupOperation("subscribe_closed_group", closedGroupPublicKey, publicKey)
@ -113,7 +113,7 @@ object PushManagerV1 {
fun unsubscribeGroup( fun unsubscribeGroup(
closedGroupPublicKey: String, closedGroupPublicKey: String,
isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), isUsingFCM: Boolean = TextSecurePreferences.isPushEnabled(context),
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!! publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
) = if (isUsingFCM) { ) = if (isUsingFCM) {
performGroupOperation("unsubscribe_closed_group", closedGroupPublicKey, publicKey) performGroupOperation("unsubscribe_closed_group", closedGroupPublicKey, publicKey)

View File

@ -37,12 +37,12 @@ interface TextSecurePreferences {
fun setLastConfigurationSyncTime(value: Long) fun setLastConfigurationSyncTime(value: Long)
fun getConfigurationMessageSynced(): Boolean fun getConfigurationMessageSynced(): Boolean
fun setConfigurationMessageSynced(value: Boolean) fun setConfigurationMessageSynced(value: Boolean)
fun isUsingFCM(): Boolean fun isPushEnabled(): Boolean
fun setIsUsingFCM(value: Boolean) fun setPushEnabled(value: Boolean)
fun getFCMToken(): String? fun getPushToken(): String?
fun setFCMToken(value: String) fun setPushToken(value: String)
fun getLastFCMUploadTime(): Long fun getPushRegisterTime(): Long
fun setLastFCMUploadTime(value: Long) fun setPushRegisterTime(value: Long)
fun isScreenLockEnabled(): Boolean fun isScreenLockEnabled(): Boolean
fun setScreenLockEnabled(value: Boolean) fun setScreenLockEnabled(value: Boolean)
fun getScreenLockTimeout(): Long fun getScreenLockTimeout(): Long
@ -251,9 +251,9 @@ interface TextSecurePreferences {
const val LINK_PREVIEWS = "pref_link_previews" const val LINK_PREVIEWS = "pref_link_previews"
const val GIF_METADATA_WARNING = "has_seen_gif_metadata_warning" const val GIF_METADATA_WARNING = "has_seen_gif_metadata_warning"
const val GIF_GRID_LAYOUT = "pref_gif_grid_layout" 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 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 LAST_CONFIGURATION_SYNC_TIME = "pref_last_configuration_sync_time"
const val CONFIGURATION_SYNCED = "pref_configuration_synced" const val CONFIGURATION_SYNCED = "pref_configuration_synced"
const val LAST_PROFILE_UPDATE_TIME = "pref_last_profile_update_time" const val LAST_PROFILE_UPDATE_TIME = "pref_last_profile_update_time"
@ -309,13 +309,13 @@ interface TextSecurePreferences {
} }
@JvmStatic @JvmStatic
fun isUsingFCM(context: Context): Boolean { fun isPushEnabled(context: Context): Boolean {
return getBooleanPreference(context, IS_USING_FCM, false) return getBooleanPreference(context, IS_PUSH_ENABLED, false)
} }
@JvmStatic @JvmStatic
fun setIsUsingFCM(context: Context, value: Boolean) { fun setPushEnabled(context: Context, value: Boolean) {
setBooleanPreference(context, IS_USING_FCM, value) setBooleanPreference(context, IS_PUSH_ENABLED, value)
} }
@JvmStatic @JvmStatic
@ -329,11 +329,11 @@ interface TextSecurePreferences {
} }
fun getLastFCMUploadTime(context: Context): Long { 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) { fun setLastFCMUploadTime(context: Context, value: Long) {
setLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, value) setLongPreference(context, PUSH_REGISTER_TIME, value)
} }
// endregion // endregion
@ -1032,28 +1032,28 @@ class AppTextSecurePreferences @Inject constructor(
TextSecurePreferences._events.tryEmit(TextSecurePreferences.CONFIGURATION_SYNCED) TextSecurePreferences._events.tryEmit(TextSecurePreferences.CONFIGURATION_SYNCED)
} }
override fun isUsingFCM(): Boolean { override fun isPushEnabled(): Boolean {
return getBooleanPreference(TextSecurePreferences.IS_USING_FCM, false) return getBooleanPreference(TextSecurePreferences.IS_PUSH_ENABLED, false)
} }
override fun setIsUsingFCM(value: Boolean) { override fun setPushEnabled(value: Boolean) {
setBooleanPreference(TextSecurePreferences.IS_USING_FCM, value) setBooleanPreference(TextSecurePreferences.IS_PUSH_ENABLED, value)
} }
override fun getFCMToken(): String? { override fun getPushToken(): String? {
return getStringPreference(TextSecurePreferences.FCM_TOKEN, "") return getStringPreference(TextSecurePreferences.FCM_TOKEN, "")
} }
override fun setFCMToken(value: String) { override fun setPushToken(value: String) {
setStringPreference(TextSecurePreferences.FCM_TOKEN, value) setStringPreference(TextSecurePreferences.FCM_TOKEN, value)
} }
override fun getLastFCMUploadTime(): Long { override fun getPushRegisterTime(): Long {
return getLongPreference(TextSecurePreferences.LAST_FCM_TOKEN_UPLOAD_TIME, 0) return getLongPreference(TextSecurePreferences.PUSH_REGISTER_TIME, 0)
} }
override fun setLastFCMUploadTime(value: Long) { override fun setPushRegisterTime(value: Long) {
setLongPreference(TextSecurePreferences.LAST_FCM_TOKEN_UPLOAD_TIME, value) setLongPreference(TextSecurePreferences.PUSH_REGISTER_TIME, value)
} }
override fun isScreenLockEnabled(): Boolean { override fun isScreenLockEnabled(): Boolean {