Skip to content

Commit 30d0dc3

Browse files
authored
feat(notifications): add notifs FCM Firebase (#51)
1 parent 6b234a0 commit 30d0dc3

File tree

8 files changed

+94
-457
lines changed

8 files changed

+94
-457
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,43 @@
2121
android:networkSecurityConfig="@xml/network_security"
2222
android:usesCleartextTraffic="true"
2323
android:enableOnBackInvokedCallback="true"
24-
tools:targetApi="31">
24+
tools:targetApi="33">
2525

26-
<receiver
27-
android:name=".ui.notifications.AlarmReceiver"
28-
android:enabled="true" />
26+
<!-- FCM default notification icon and color -->
27+
<meta-data
28+
android:name="com.google.firebase.messaging.default_notification_icon"
29+
android:resource="@drawable/app_icon_cropped" />
30+
<meta-data
31+
android:name="com.google.firebase.messaging.default_notification_color"
32+
android:resource="@color/xpeho_color" />
2933

3034
<activity
3135
android:name=".MainActivity"
3236
android:exported="true"
3337
android:theme="@style/Theme.XpeAppSplashScreen"
34-
android:windowSoftInputMode="adjustResize">
38+
android:windowSoftInputMode="adjustResize"
39+
android:launchMode="singleTop">
3540
<intent-filter>
3641
<action android:name="android.intent.action.MAIN" />
3742

3843
<category android:name="android.intent.category.LAUNCHER" />
3944
</intent-filter>
4045

46+
<!-- Intent filter for FCM notification click_action -->
47+
<intent-filter>
48+
<action android:name="OPEN_XPEAPP" />
49+
<category android:name="android.intent.category.DEFAULT" />
50+
</intent-filter>
4151
</activity>
52+
53+
<!-- Service -->
54+
<service
55+
android:name=".data.service.FirebaseCloudMessagingService"
56+
android:exported="false">
57+
<intent-filter>
58+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
59+
</intent-filter>
60+
</service>
4261
</application>
4362

4463
</manifest>

app/src/main/java/com/xpeho/xpeapp/MainActivity.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
1818
import androidx.lifecycle.compose.collectAsStateWithLifecycle
1919
import com.xpeho.xpeapp.enums.Screens
2020
import com.xpeho.xpeapp.ui.Home
21-
import com.xpeho.xpeapp.ui.notifications.AlarmScheduler
2221
import com.xpeho.xpeapp.ui.theme.XpeAppTheme
2322
import kotlinx.coroutines.CoroutineScope
2423
import com.xpeho.xpeapp.utils.DefaultDispatcherProvider
@@ -33,6 +32,7 @@ import java.io.IOException
3332
import com.xpeho.xpeho_ui_android.foundations.Colors as XpehoColors
3433
import kotlin.time.Duration.Companion.hours
3534
import androidx.core.net.toUri
35+
import com.google.firebase.messaging.FirebaseMessaging
3636

3737
class MainActivity : ComponentActivity() {
3838

@@ -44,8 +44,7 @@ class MainActivity : ComponentActivity() {
4444
ActivityResultContracts.RequestPermission()
4545
) { isGranted: Boolean ->
4646
if (isGranted) {
47-
// Schedule the alarm if the permission is granted
48-
scheduleNotificationAlarm()
47+
FirebaseMessaging.getInstance().subscribeToTopic("all")
4948
} else {
5049
Log.d("Permission", "Permission denied")
5150
}
@@ -124,7 +123,7 @@ class MainActivity : ComponentActivity() {
124123
android.Manifest.permission.POST_NOTIFICATIONS
125124
) == PackageManager.PERMISSION_GRANTED -> {
126125
Log.d("Permission", "Permission already granted")
127-
scheduleNotificationAlarm()
126+
FirebaseMessaging.getInstance().subscribeToTopic("all")
128127
}
129128

130129
else -> {
@@ -133,11 +132,6 @@ class MainActivity : ComponentActivity() {
133132
}
134133
}
135134

136-
private fun scheduleNotificationAlarm() {
137-
val alarmScheduler = AlarmScheduler()
138-
alarmScheduler.scheduleAlarm(this)
139-
}
140-
141135
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
142136
private fun checkForUpdate() {
143137
// Check for updates only in release mode

app/src/main/java/com/xpeho/xpeapp/XpeApp.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class XpeApp : Application() {
1212

1313
override fun onCreate() {
1414
super.onCreate()
15-
15+
1616
// Initialize Firebase Crashlytics
1717
FirebaseCrashlytics.getInstance().isCrashlyticsCollectionEnabled = true
18-
18+
1919
appModule = MainAppModule(appContext = this)
2020
}
2121
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.xpeho.xpeapp.data.service
2+
3+
import android.app.NotificationManager
4+
import android.app.PendingIntent
5+
import android.content.Intent
6+
import androidx.core.app.NotificationCompat
7+
import com.google.firebase.messaging.FirebaseMessagingService
8+
import com.google.firebase.messaging.RemoteMessage
9+
import com.xpeho.xpeapp.MainActivity
10+
import com.xpeho.xpeapp.R
11+
import com.xpeho.xpeapp.utils.CrashlyticsUtils
12+
13+
class FirebaseCloudMessagingService : FirebaseMessagingService() {
14+
15+
companion object {
16+
private const val CHANNEL_ID = "XpeApp_Notifications"
17+
}
18+
19+
override fun onNewToken(token: String) {
20+
super.onNewToken(token)
21+
CrashlyticsUtils.logEvent("FCM: Nouveau token reçu")
22+
}
23+
24+
override fun onMessageReceived(remoteMessage: RemoteMessage) {
25+
super.onMessageReceived(remoteMessage)
26+
CrashlyticsUtils.logEvent("FCM: Message reçu")
27+
28+
val title = remoteMessage.notification?.title ?: remoteMessage.data["title"] ?: "XpeApp"
29+
val message = remoteMessage.notification?.body ?: remoteMessage.data["message"] ?: ""
30+
31+
if (message.isNotEmpty()) {
32+
showNotification(title, message)
33+
}
34+
}
35+
36+
private fun showNotification(title: String, message: String) {
37+
val notificationManager = getSystemService(NotificationManager::class.java)
38+
39+
// Intent pour ouvrir l'app au clic
40+
val intent = Intent(this, MainActivity::class.java).apply {
41+
action = "OPEN_XPEAPP"
42+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
43+
}
44+
val pendingIntent = PendingIntent.getActivity(
45+
this,
46+
0,
47+
intent,
48+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
49+
)
50+
51+
// Construire la notification
52+
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
53+
.setContentTitle(title)
54+
.setContentText(message)
55+
.setSmallIcon(R.drawable.app_icon_cropped)
56+
.setPriority(NotificationCompat.PRIORITY_HIGH)
57+
.setStyle(NotificationCompat.BigTextStyle().bigText(message))
58+
.setAutoCancel(true) // Fermer la notification au clic
59+
.setContentIntent(pendingIntent) // Ouvrir l'app au clic
60+
.build()
61+
62+
val notificationId = System.currentTimeMillis().toInt()
63+
notificationManager.notify(notificationId, notification)
64+
}
65+
}

app/src/main/java/com/xpeho/xpeapp/ui/components/NewsletterNotification.kt

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)