Use firebaseReceiver with FCM

This commit is contained in:
sim 2022-09-07 18:02:26 +02:00
parent 9b5d8c2d01
commit 9cf6dd7a88
8 changed files with 95 additions and 53 deletions

View File

@ -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"
<service android:name="im.vector.app.push.fcm.FirebaseReceiver"
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>

View File

@ -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'

View File

@ -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"

View File

@ -1,27 +0,0 @@
/*
* 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 android.content.Context
import org.unifiedpush.android.embedded_fcm_distributor.EmbeddedDistributorReceiver
class EmbeddedFCMDistributor : EmbeddedDistributorReceiver() {
override fun getEndpoint(context: Context, token: String, instance: String): String {
// Here token is the FCM Token, used by the gateway (sygnal)
return token
}
}

View File

@ -0,0 +1,59 @@
/*
* 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 android.content.Intent
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.PushersManager
import im.vector.app.features.settings.VectorPreferences
import org.json.JSONObject
import org.unifiedpush.android.connector.ACTION_MESSAGE
import org.unifiedpush.android.connector.EXTRA_BYTES_MESSAGE
import org.unifiedpush.android.connector.EXTRA_TOKEN
import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint
class FirebaseReceiver : FirebaseMessagingService() {
@Inject lateinit var fcmHelper: FcmHelper
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var pushersManager: PushersManager
override fun onNewToken(token: String) {
Timber.d("New Firebase token")
fcmHelper.storeFcmToken(token)
if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) {
pushersManager.enqueueRegisterPusher(token, getString(R.string.default_push_gateway_http_url))
}
}
override fun onMessageReceived(message: RemoteMessage) {
Timber.d("New Firebase message")
val intent = Intent()
intent.action = ACTION_MESSAGE
intent.setPackage(baseContext.packageName)
intent.putExtra(EXTRA_BYTES_MESSAGE, JSONObject(message.data as Map<*, *>).toString().toByteArray())
intent.putExtra(EXTRA_TOKEN, fcmHelper.getFcmToken())
baseContext.sendBroadcast(intent)
}
}

View File

@ -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>

View File

@ -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

View File

@ -46,6 +46,8 @@ import org.matrix.android.sdk.api.logger.LoggerTag
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.getRoom
import org.matrix.android.sdk.api.session.room.getTimelineEvent
import org.unifiedpush.android.connector.EXTRA_BYTES_MESSAGE
import org.unifiedpush.android.connector.EXTRA_TOKEN
import org.unifiedpush.android.connector.MessagingReceiver
import timber.log.Timber
import javax.inject.Inject
@ -60,6 +62,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
@Inject lateinit var notificationDrawerManager: NotificationDrawerManager
@Inject lateinit var notifiableEventResolver: NotifiableEventResolver
@Inject lateinit var pushersManager: PushersManager
@Inject lateinit var fcmHelper: FcmHelper
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var vectorDataStore: VectorDataStore
@ -166,6 +169,19 @@ class VectorMessagingReceiver : MessagingReceiver() {
}
}
override fun onReceive(context: Context, intent: Intent) {
// Injections happens here
super.onReceive(context, intent)
// if it is from FirebaseReceiver, then the token has been rejected
if (unifiedPushHelper.isEmbeddedDistributor()
&& intent.getStringExtra(EXTRA_TOKEN) == fcmHelper.getFcmToken()) {
intent.getByteArrayExtra(EXTRA_BYTES_MESSAGE)?.let {
onMessage(context, it, "")
}
}
}
/**
* Internal receive method.
*