Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use firebaseReceiver with FCM #7068

Merged
merged 22 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6936.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Smaff refactor of UnifiedPushHelper
1 change: 1 addition & 0 deletions changelog.d/7068.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix push with FCM
14 changes: 4 additions & 10 deletions vector-app/src/gplay/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@
android:name="firebase_analytics_collection_deactivated"
android:value="true" />

<receiver
android:name="im.vector.app.push.fcm.EmbeddedFCMDistributor"
android:enabled="true"
android:exported="false">

<service android:name="im.vector.app.push.fcm.VectorFirebaseMessagingService"
android:exported="false" >
<intent-filter>
<action android:name="org.unifiedpush.android.distributor.REGISTER" />
<action android:name="org.unifiedpush.android.distributor.UNREGISTER" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>

</receiver>

</service>
</application>

</manifest>
2 changes: 1 addition & 1 deletion vector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ dependencies {
// UnifiedPush
implementation 'com.github.UnifiedPush:android-connector:2.0.1'
// UnifiedPush gplay flavor only
gplayImplementation('com.github.UnifiedPush:android-embedded_fcm_distributor:2.1.3') {
gplayImplementation('com.google.firebase:firebase-messaging:23.0.8') {
exclude group: 'com.google.firebase', module: 'firebase-core'
exclude group: 'com.google.firebase', module: 'firebase-analytics'
exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
Expand Down
14 changes: 0 additions & 14 deletions vector/src/fdroid/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@
android:enabled="true"
android:exported="false" />

<receiver
android:name=".fdroid.receiver.KeepInternalDistributor"
android:enabled="true"
android:exported="false">
<intent-filter>
<!--
This action is checked to track installed and uninstalled distributors.
We declare it to keep the background sync as an internal
unifiedpush distributor.
-->
<action android:name="org.unifiedpush.android.distributor.REGISTER" />
</intent-filter>
</receiver>

<service
android:name=".fdroid.service.GuardAndroidService"
android:exported="false"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2022 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.push.fcm

import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.pushers.FcmHelper
import im.vector.app.core.pushers.PushParser
import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.core.pushers.VectorPushHandler
import im.vector.app.features.settings.VectorPreferences
import org.matrix.android.sdk.api.logger.LoggerTag
import timber.log.Timber
import javax.inject.Inject

private val loggerTag = LoggerTag("Push", LoggerTag.SYNC)

@AndroidEntryPoint
class VectorFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var fcmHelper: FcmHelper
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var pushersManager: PushersManager
@Inject lateinit var pushParser: PushParser
@Inject lateinit var vectorPushHandler: VectorPushHandler
@Inject lateinit var unifiedPushHelper: UnifiedPushHelper

override fun onNewToken(token: String) {
Timber.tag(loggerTag.value).d("New Firebase token")
fcmHelper.storeFcmToken(token)
if (
vectorPreferences.areNotificationEnabledForDevice() &&
activeSessionHolder.hasActiveSession() &&
unifiedPushHelper.isEmbeddedDistributor()
) {
pushersManager.enqueueRegisterPusher(token, getString(R.string.pusher_http_url))
}
}

override fun onMessageReceived(message: RemoteMessage) {
Timber.tag(loggerTag.value).d("New Firebase message")
pushParser.parsePushDataFcm(message.data)?.let {
vectorPushHandler.handle(it)
} ?: run {
Timber.tag(loggerTag.value).w("Invalid received data Json format")
}
p1gp1g marked this conversation as resolved.
Show resolved Hide resolved
}
}
16 changes: 15 additions & 1 deletion vector/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@

<!-- UnifiedPush -->
<receiver
android:name=".core.pushers.VectorMessagingReceiver"
android:name=".core.pushers.VectorUnifiedPushMessagingReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
Expand All @@ -423,6 +423,20 @@
<action android:name="org.unifiedpush.android.connector.REGISTRATION_REFUSED" />
</intent-filter>
</receiver>

<receiver
android:name=".core.pushers.KeepInternalDistributor"
android:enabled="true"
android:exported="false">
<intent-filter>
<!--
This action is checked to track installed and uninstalled distributors.
We declare it to keep the background sync as an internal
unifiedpush distributor.
-->
<action android:name="org.unifiedpush.android.distributor.REGISTER" />
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package im.vector.app.fdroid.receiver
package im.vector.app.core.pushers

import android.content.BroadcastReceiver
import android.content.Context
Expand Down
16 changes: 10 additions & 6 deletions vector/src/main/java/im/vector/app/core/pushers/PushParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import im.vector.app.core.pushers.model.PushData
import im.vector.app.core.pushers.model.PushDataFcm
import im.vector.app.core.pushers.model.PushDataUnifiedPush
import im.vector.app.core.pushers.model.toPushData
import org.json.JSONObject
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.util.MatrixJsonParser
import javax.inject.Inject
Expand All @@ -40,12 +41,15 @@ class PushParser @Inject constructor() {
* [3] https://spec.matrix.org/latest/push-gateway-api/
* [4] https://github.com/p1gp1g/sygnal/blob/unifiedpush/sygnal/upfcmpushkin.py (Not tested for a while)
*/
fun parseData(message: String, firebaseFormat: Boolean): PushData? {
val moshi = MatrixJsonParser.getMoshi()
return if (firebaseFormat) {
tryOrNull { moshi.adapter(PushDataFcm::class.java).fromJson(message) }?.toPushData()
} else {
tryOrNull { moshi.adapter(PushDataUnifiedPush::class.java).fromJson(message) }?.toPushData()
fun parsePushDataUnifiedPush(message: ByteArray): PushData? {
return MatrixJsonParser.getMoshi().let {
tryOrNull { it.adapter(PushDataUnifiedPush::class.java).fromJson(String(message)) }?.toPushData()
}
}

fun parsePushDataFcm(message: Map<*, *>): PushData? {
return MatrixJsonParser.getMoshi().let {
tryOrNull { it.adapter(PushDataFcm::class.java).fromJson(JSONObject(message).toString()) }?.toPushData()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import kotlin.math.abs
private const val DEFAULT_PUSHER_FILE_TAG = "mobile"

class PushersManager @Inject constructor(
private val unifiedPushStore: UnifiedPushStore,
private val unifiedPushHelper: UnifiedPushHelper,
private val activeSessionHolder: ActiveSessionHolder,
private val localeProvider: LocaleProvider,
private val stringProvider: StringProvider,
Expand All @@ -39,9 +39,9 @@ class PushersManager @Inject constructor(
val currentSession = activeSessionHolder.getActiveSession()

currentSession.pushersService().testPush(
unifiedPushStore.getPushGateway()!!,
unifiedPushHelper.getPushGateway() ?: return,
stringProvider.getString(R.string.pusher_app_id),
unifiedPushStore.getEndpointOrToken().orEmpty(),
unifiedPushHelper.getEndpointOrToken().orEmpty(),
TEST_EVENT_ID
)
}
Expand Down
Loading