Skip to content

Add support for UnifiedPush notifications #5261

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions app/gradle.lockfile

Large diffs are not rendered by default.

51 changes: 31 additions & 20 deletions app/lint-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
errorLine2=" ~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/launch/LaunchActivity.kt"
line="46"
line="49"
column="7"/>
</issue>

Expand Down Expand Up @@ -624,6 +624,17 @@
column="10"/>
</issue>

<issue
id="ExportedReceiver"
message="Exported receiver does not require permission"
errorLine1=" &lt;receiver"
errorLine2=" ~~~~~~~~">
<location
file="src/main/AndroidManifest.xml"
line="930"
column="10"/>
</issue>

<issue
id="ExportedService"
message="Exported service does not require permission"
Expand Down Expand Up @@ -708,7 +719,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt"
line="478"
line="480"
column="17"/>
</issue>

Expand All @@ -719,7 +730,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt"
line="478"
line="480"
column="17"/>
</issue>

Expand Down Expand Up @@ -1227,7 +1238,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1454"
line="1498"
column="16"/>
</issue>

Expand All @@ -1238,7 +1249,7 @@
errorLine2=" ^">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1584"
line="1628"
column="33"/>
</issue>

Expand All @@ -1249,7 +1260,7 @@
errorLine2=" ^">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1584"
line="1628"
column="33"/>
</issue>

Expand All @@ -1260,7 +1271,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1619"
line="1663"
column="41"/>
</issue>

Expand All @@ -1271,7 +1282,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1619"
line="1663"
column="41"/>
</issue>

Expand All @@ -1282,7 +1293,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1659"
line="1703"
column="13"/>
</issue>

Expand All @@ -1293,7 +1304,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1675"
line="1719"
column="13"/>
</issue>

Expand All @@ -1304,7 +1315,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1684"
line="1728"
column="23"/>
</issue>

Expand All @@ -1315,7 +1326,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1684"
line="1728"
column="23"/>
</issue>

Expand All @@ -1326,7 +1337,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1802"
line="1846"
column="68"/>
</issue>

Expand All @@ -1337,7 +1348,7 @@
errorLine2=" ^">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1871"
line="1915"
column="37"/>
</issue>

Expand All @@ -1348,7 +1359,7 @@
errorLine2=" ^">
<location
file="src/main/kotlin/io/homeassistant/companion/android/notifications/MessagingManager.kt"
line="1871"
line="1915"
column="37"/>
</issue>

Expand Down Expand Up @@ -1403,7 +1414,7 @@
errorLine2=" ~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt"
line="308"
line="310"
column="52"/>
</issue>

Expand All @@ -1414,7 +1425,7 @@
errorLine2=" ~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt"
line="308"
line="310"
column="52"/>
</issue>

Expand All @@ -1425,7 +1436,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt"
line="329"
line="331"
column="52"/>
</issue>

Expand All @@ -1436,7 +1447,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt"
line="329"
line="331"
column="52"/>
</issue>

Expand All @@ -1447,7 +1458,7 @@
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt"
line="511"
line="537"
column="21"/>
</issue>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.data.integration.DeviceRegistration
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.onboarding.getMessagingToken
import io.homeassistant.companion.android.util.tryRegisterCurrentDistributor
import javax.inject.Inject
import kotlinx.coroutines.launch
import org.unifiedpush.android.connector.UnifiedPush
import timber.log.Timber

@ActivityScoped
Expand All @@ -21,11 +23,20 @@ class LaunchPresenterImpl @Inject constructor(
serverManager.defaultServers.forEach {
ioScope.launch {
try {
// Don't get a new push token if using UnifiedPush.
val messagingToken = if (!UnifiedPush.tryRegisterCurrentDistributor(view as Context)) {
getMessagingToken()
} else {
null
}
serverManager.integrationRepository(it.id).updateRegistration(
DeviceRegistration(
"${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
null,
getMessagingToken()
appVersion = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
deviceName = null,
pushToken = messagingToken,
// A blank url indicates to use the build-time push url.
pushUrl = messagingToken?.let { "" },
pushEncrypt = messagingToken == null && UnifiedPush.getAckDistributor(view as Context) != null
)
)
serverManager.integrationRepository(it.id).getConfig() // Update cached data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class FirebaseCloudMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
mainScope.launch {
Timber.d("Refreshed token: $token")
if (messagingManager.isUnifiedPushEnabled()) {
// Updating registration while using UnifiedPush will overwrite its token, so ignore new FCM tokens.
Timber.d("Not trying to update registration since UnifiedPush is being used.")
return@launch
}
if (!serverManager.isRegistered()) {
Timber.d("Not trying to update registration since we aren't authenticated.")
return@launch
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,18 @@
android:value="true" />
</service>

<receiver
android:name=".unifiedpush.UnifiedPushReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="org.unifiedpush.android.connector.MESSAGE"/>
<action android:name="org.unifiedpush.android.connector.UNREGISTERED"/>
<action android:name="org.unifiedpush.android.connector.NEW_ENDPOINT"/>
<action android:name="org.unifiedpush.android.connector.REGISTRATION_FAILED"/>
</intent-filter>
</receiver>

<receiver
android:name=".notifications.NotificationActionReceiver"
android:enabled="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.integration.DeviceRegistration
import io.homeassistant.companion.android.common.data.prefs.PrefsRepository
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.database.sensor.SensorDao
import io.homeassistant.companion.android.database.server.Server
Expand All @@ -31,6 +32,7 @@ import io.homeassistant.companion.android.settings.SettingViewModel
import io.homeassistant.companion.android.settings.server.ServerChooserFragment
import io.homeassistant.companion.android.util.UrlUtil
import io.homeassistant.companion.android.util.compose.HomeAssistantAppTheme
import io.homeassistant.companion.android.util.tryRegisterCurrentOrDefaultDistributor
import io.homeassistant.companion.android.webview.WebViewActivity
import javax.inject.Inject
import javax.net.ssl.SSLException
Expand All @@ -39,6 +41,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.unifiedpush.android.connector.UnifiedPush
import retrofit2.HttpException
import timber.log.Timber

Expand All @@ -54,6 +57,9 @@ class LaunchActivity : AppCompatActivity(), LaunchView {
@Inject
lateinit var sensorDao: SensorDao

@Inject
lateinit var prefsRepository: PrefsRepository

private val mainScope = CoroutineScope(Dispatchers.Main + Job())

private val settingViewModel: SettingViewModel by viewModels()
Expand Down Expand Up @@ -138,8 +144,13 @@ class LaunchActivity : AppCompatActivity(), LaunchView {
mainScope.launch {
if (result != null) {
val (url, authCode, deviceName, deviceTrackingEnabled, notificationsEnabled) = result
val messagingToken = getMessagingToken()
if (messagingToken.isBlank() && BuildConfig.FLAVOR == "full") {
// Try UnifiedPush first, then fallback to FCM token.
val messagingToken = if (!UnifiedPush.tryRegisterCurrentOrDefaultDistributor(this@LaunchActivity)) {
getMessagingToken()
} else {
null
}
if (messagingToken != null && messagingToken.isBlank() && BuildConfig.FLAVOR == "full") {
AlertDialog.Builder(this@LaunchActivity)
.setTitle(commonR.string.firebase_error_title)
.setMessage(commonR.string.firebase_error_message)
Expand Down Expand Up @@ -176,7 +187,7 @@ class LaunchActivity : AppCompatActivity(), LaunchView {
url: String,
authCode: String,
deviceName: String,
messagingToken: String,
messagingToken: String?,
deviceTrackingEnabled: Boolean,
notificationsEnabled: Boolean
) {
Expand All @@ -196,12 +207,16 @@ class LaunchActivity : AppCompatActivity(), LaunchView {
serverManager.authenticationRepository(serverId).registerAuthorizationCode(authCode)
serverManager.integrationRepository(serverId).registerDevice(
DeviceRegistration(
"${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
deviceName,
messagingToken
appVersion = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
deviceName = deviceName,
pushToken = messagingToken,
// A blank url indicates to use the build-time push url.
pushUrl = messagingToken?.let { "" },
pushEncrypt = messagingToken == null && !UnifiedPush.getAckDistributor(this.applicationContext).isNullOrBlank()
)
)
serverId = serverManager.convertTemporaryServer(serverId)
prefsRepository.setUnifiedPushEnabled(messagingToken == null)
} catch (e: Exception) {
// Fatal errors: if one of these calls fail, the app cannot proceed.
// Show an error, clean up the session and require new registration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

abstract class LaunchPresenterBase(
private val view: LaunchView,
internal val view: LaunchView,
internal val serverManager: ServerManager
) : LaunchPresenter {

Expand Down
Loading