1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-3330 notifications

This commit is contained in:
konstantiniiv 2025-02-04 09:16:23 +01:00
parent 1302da7357
commit 74ce176540
6 changed files with 100 additions and 1 deletions

View file

@ -4,6 +4,7 @@ plugins {
id 'com.google.devtools.ksp'
alias(libs.plugins.compose.compiler)
id "com.google.firebase.appdistribution"
id 'com.google.gms.google-services'
}
if (rootProject.ext.onCi) {
@ -243,6 +244,9 @@ dependencies {
implementation libs.wireRuntime
implementation platform('com.google.firebase:firebase-bom:33.8.0')
implementation "com.google.firebase:firebase-messaging"
//Unit/Integration tests dependencies
testImplementation libs.androidXTestCore
testImplementation libs.junit

View file

@ -2,7 +2,7 @@
"project_info": {
"project_number": "665406949256",
"project_id": "anytype-android-debug",
"storage_bucket": "anytype-android-debug.appspot.com"
"storage_bucket": "anytype-android-debug.firebasestorage.app"
},
"client": [
{

View file

@ -144,6 +144,14 @@
</intent-filter>
<meta-data android:name="photopicker_activity:0:required" android:value="" />
</service>
<service
android:name="com.anytypeio.anytype.other.AnytypeFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>

View file

@ -0,0 +1,57 @@
package com.anytypeio.anytype.other
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import com.anytypeio.anytype.R
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
class AnytypeFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
// This callback is fired whenever a new token is generated for the device.
// Send this token to your app server for registration.
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
// Check if message contains a data payload.
remoteMessage.data.let { data ->
// Handle data
}
// Check if message contains a notification payload.
remoteMessage.notification?.let { notification ->
val title = notification.title
val body = notification.body
// Display the notification
sendNotification(title, body)
}
}
private fun sendNotification(title: String?, body: String?) {
// Build and show a notification via NotificationManager
val channelId = "fcm_default_channel"
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_anytype_qr_code_logo)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Create channel for Android Oreo and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel(channelId, "FCM Channel", NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build())
}
}

View file

@ -30,6 +30,9 @@ dependencies {
implementation libs.lifecycleViewModel
implementation libs.lifecycleRuntime
implementation platform('com.google.firebase:firebase-bom:33.8.0')
implementation "com.google.firebase:firebase-messaging"
implementation libs.appcompat
implementation libs.compose
implementation libs.composeFoundation

View file

@ -7,9 +7,12 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
@ -23,7 +26,9 @@ import com.anytypeio.anytype.core_ui.views.Caption1Regular
import com.anytypeio.anytype.core_ui.views.Caption2Regular
import com.anytypeio.anytype.core_ui.views.Title1
import com.anytypeio.anytype.core_ui.views.UXBody
import com.anytypeio.anytype.ui_settings.BuildConfig
import com.anytypeio.anytype.ui_settings.R
import com.google.firebase.messaging.FirebaseMessaging
@Composable
fun AboutAppScreen(
@ -121,6 +126,28 @@ fun AboutAppScreen(
)
)
}
val tokenState = remember { mutableStateOf<String?>(null) }
if (BuildConfig.DEBUG) {
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
if (!task.isSuccessful) {
return@addOnCompleteListener
}
tokenState.value = task.result
}
if (tokenState.value != null) {
SelectionContainer {
Text(
modifier = Modifier
.padding(horizontal = 20.dp)
.padding(bottom = 10.dp),
text = "FCM Token: ${tokenState.value}",
style = Caption2Regular.copy(
color = colorResource(id = R.color.text_secondary)
)
)
}
}
}
}
}