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

Update firebase to latest version #3135

Merged
merged 2 commits into from
May 28, 2024
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
2 changes: 1 addition & 1 deletion mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ dependencies {
implementation "com.mikepenz:aboutlibraries:$about_libraries_version"

// Firebase
implementation platform("com.google.firebase:firebase-bom:25.13.0")
implementation platform("com.google.firebase:firebase-bom:33.0.0")
fullImplementation "com.google.firebase:firebase-messaging-ktx"
fullImplementation "com.google.firebase:firebase-crashlytics-ktx"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ object NotificationPoller {

newMessages.forEach { message ->
notifHelper.showNotification(
message.id.hashCode(),
message,
null,
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ object CloudMessagingHelper {
}
}

fun onNotificationSelected(context: Context, intent: Intent) {
val notificationId = intent.getIntExtra(NotificationHelper.EXTRA_NOTIFICATION_ID, -1)
if (notificationId >= 0) {
FcmRegistrationWorker.scheduleHideNotification(context, notificationId)
}
}
@Suppress("UNUSED_PARAMETER")
fun onNotificationSelected(context: Context, intent: Intent) {}

fun isPollingBuild() = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class FcmMessageListenerService : FirebaseMessagingService() {
val data = message.data
Log.d(TAG, "onMessageReceived with data $data")
val messageType = data["type"] ?: return
val notificationId = data["notificationId"]?.toInt() ?: 1

when (messageType) {
"notification" -> {
Expand All @@ -55,20 +54,15 @@ class FcmMessageListenerService : FirebaseMessagingService() {
)

runBlocking {
val context = this@FcmMessageListenerService
notifHelper.showNotification(
notificationId,
cloudNotification,
FcmRegistrationWorker.createHideNotificationIntent(context, notificationId),
FcmRegistrationWorker.createHideNotificationIntent(
context,
NotificationHelper.SUMMARY_NOTIFICATION_ID
)
null,
null
)
}
}
"hideNotification" -> {
notifHelper.cancelNotification(notificationId)
notifHelper.cancelNotification(data["persistedId"].orEmpty().hashCode())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.WorkRequest
import androidx.work.WorkerParameters
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.RemoteMessage
import java.io.IOException
import java.net.URLEncoder
import java.util.Locale
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.runBlocking
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.CloudConnection
import org.openhab.habdroid.core.connection.ConnectionFactory
Expand Down Expand Up @@ -100,13 +99,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
return retryOrFail()
}
}
ACTION_HIDE_NOTIFICATION -> {
val id = inputData.getInt(KEY_NOTIFICATION_ID, -1)
if (id >= 0) {
sendHideNotificationRequest(id, connection.messagingSenderId)
return Result.success()
}
}
else -> Log.e(TAG, "Invalid action '$action'")
}

Expand All @@ -121,34 +113,25 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
// HttpException is thrown by our HTTP code, IOException can be thrown by FCM
@Throws(HttpClient.HttpException::class, IOException::class)
private suspend fun registerFcm(connection: CloudConnection) {
val token = FirebaseInstanceId.getInstance().getToken(
connection.messagingSenderId,
FirebaseMessaging.INSTANCE_ID_SCOPE
)
val deviceName = deviceName + if (Util.isFlavorBeta) " (${context.getString(R.string.beta)})" else ""
val deviceId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) +
if (Util.isFlavorBeta) "-beta" else ""

val regUrl = String.format(
Locale.US,
"addAndroidRegistration?deviceId=%s&deviceModel=%s&regId=%s",
deviceId,
URLEncoder.encode(deviceName, "UTF-8"),
token
)

Log.d(TAG, "Register device at openHAB-cloud with URL: $regUrl")
connection.httpClient.get(regUrl).close()
Log.d(TAG, "FCM reg id success")
}

private fun sendHideNotificationRequest(notificationId: Int, senderId: String) {
val fcm = FirebaseMessaging.getInstance()
val message = RemoteMessage.Builder("$senderId@gcm.googleapis.com")
.addData("type", "hideNotification")
.addData("notificationId", notificationId.toString())
.build()
fcm.send(message)
FirebaseMessaging.getInstance().token.addOnSuccessListener { token: String ->
val deviceName = deviceName + if (Util.isFlavorBeta) " (${context.getString(R.string.beta)})" else ""
val deviceId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) +
if (Util.isFlavorBeta) "-beta" else ""

val registrationUrl = String.format(
Locale.US,
"addAndroidRegistration?deviceId=%s&deviceModel=%s&regId=%s",
deviceId,
URLEncoder.encode(deviceName, "UTF-8"),
token
)

Log.d(TAG, "Register device at openHAB cloud with URL: $registrationUrl")
runBlocking {
connection.httpClient.get(registrationUrl).close()
}
Log.d(TAG, "FCM reg id success")
}
}

class ProxyReceiver : BroadcastReceiver() {
Expand Down Expand Up @@ -181,7 +164,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
private val TAG = FcmRegistrationWorker::class.java.simpleName

private const val ACTION_REGISTER = "org.openhab.habdroid.action.REGISTER_GCM"
private const val ACTION_HIDE_NOTIFICATION = "org.openhab.habdroid.action.HIDE_NOTIFICATION"
private const val KEY_ACTION = "action"
private const val KEY_NOTIFICATION_ID = "notificationId"

Expand All @@ -193,23 +175,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
enqueueFcmWorker(context, data)
}

internal fun scheduleHideNotification(context: Context, notificationId: Int) {
val data = Data.Builder()
.putString(KEY_ACTION, ACTION_HIDE_NOTIFICATION)
.putInt(KEY_NOTIFICATION_ID, notificationId)
.build()

enqueueFcmWorker(context, data)
}

internal fun createHideNotificationIntent(context: Context, notificationId: Int): PendingIntent {
val intent = Intent(context, FcmRegistrationWorker::class.java)
.setAction(ACTION_HIDE_NOTIFICATION)
.putExtra(KEY_NOTIFICATION_ID, notificationId)

return ProxyReceiver.wrap(context, intent, notificationId)
}

private fun enqueueFcmWorker(context: Context, data: Data) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ class NotificationHelper(private val context: Context) {
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

suspend fun showNotification(
notificationId: Int,
message: CloudNotification,
deleteIntent: PendingIntent?,
summaryDeleteIntent: PendingIntent?
) {
createChannelForSeverity(message.severity)

val n = makeNotification(message, notificationId, deleteIntent)
val n = makeNotification(message, message.idHash, deleteIntent)

notificationManager.notify(notificationId, n)
notificationManager.notify(message.idHash, n)

if (HAS_GROUPING_SUPPORT) {
val count = countCloudNotifications(notificationManager.activeNotifications)
Expand Down Expand Up @@ -218,7 +217,6 @@ class NotificationHelper(private val context: Context) {
val contentIntent = Intent(context, MainActivity::class.java).apply {
action = MainActivity.ACTION_NOTIFICATION_SELECTED
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(EXTRA_NOTIFICATION_ID, notificationId)
putExtra(MainActivity.EXTRA_PERSISTED_NOTIFICATION_ID, persistedId)
}
return PendingIntent.getActivity(
Expand Down Expand Up @@ -249,8 +247,6 @@ class NotificationHelper(private val context: Context) {
"severity-$severity"
}

@Suppress("MemberVisibilityCanBePrivate") // Used in full flavor
internal const val EXTRA_NOTIFICATION_ID = "notificationId"
internal const val SUMMARY_NOTIFICATION_ID = 0

// Notification grouping is only available on N or higher, as mentioned in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ data class CloudNotification internal constructor(
val createdTimestamp: Long,
val icon: IconResource?,
val severity: String?
) : Parcelable
) : Parcelable {
val idHash get() = id.hashCode()
}

@Throws(JSONException::class)
fun JSONObject.toCloudNotification(): CloudNotification {
Expand Down