-
Notifications
You must be signed in to change notification settings - Fork 1
Notification Setup - Implement Basic FCM Service Setup (Part 1) #5
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package com.cornellappdev.hustle.data.remote | ||
|
|
||
| import android.app.NotificationChannel | ||
| import android.app.NotificationManager | ||
| import android.app.PendingIntent | ||
| import android.content.Intent | ||
| import android.os.Build | ||
| import androidx.core.app.NotificationCompat | ||
| import com.cornellappdev.hustle.MainActivity | ||
| import com.cornellappdev.hustle.R | ||
| import com.cornellappdev.hustle.data.repository.FcmTokenRepository | ||
| import com.google.firebase.messaging.FirebaseMessagingService | ||
| import com.google.firebase.messaging.RemoteMessage | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| @AndroidEntryPoint | ||
| class HustleFirebaseMessagingService : FirebaseMessagingService() { | ||
|
|
||
| @Inject | ||
| lateinit var fcmTokenRepository: FcmTokenRepository | ||
|
|
||
| @Inject | ||
| lateinit var applicationScope: CoroutineScope | ||
|
|
||
| private val notificationManager by lazy { | ||
| getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
| } | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
| createNotificationChannel() | ||
| } | ||
|
|
||
| override fun onNewToken(token: String) { | ||
| super.onNewToken(token) | ||
| applicationScope.launch { | ||
| fcmTokenRepository.updateFcmToken(token) | ||
| } | ||
| } | ||
|
|
||
| override fun onMessageReceived(message: RemoteMessage) { | ||
| super.onMessageReceived(message) | ||
| //TODO: Handle data messages for different notification types (eg. chat) | ||
| val title = message.notification?.title ?: message.data["title"] ?: "Hustle" | ||
| val body = message.notification?.body ?: message.data["body"] ?: "" | ||
|
|
||
| showNotification(title, body, message.data) | ||
| } | ||
|
|
||
| private fun createNotificationChannel() { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| val channel = NotificationChannel( | ||
| CHANNEL_ID, | ||
| "Hustle Notifications", | ||
| NotificationManager.IMPORTANCE_HIGH | ||
| ).apply { | ||
| enableVibration(true) | ||
| } | ||
| notificationManager.createNotificationChannel(channel) | ||
| } | ||
| } | ||
|
|
||
| private fun showNotification(title: String, body: String, data: Map<String, String>) { | ||
| val intent = Intent(this, MainActivity::class.java).apply { | ||
| // new task for launching activity from service and clear top to avoid multiple activity instances | ||
| flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP | ||
| data.forEach { (key, value) -> putExtra(key, value) } | ||
| } | ||
|
|
||
| // update current to ensure each notification has correct data and immutable for security best practices | ||
| val pendingIntent = PendingIntent.getActivity( | ||
| this, | ||
| data.hashCode(), | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with these flags?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar situation as above. Update current flag is to make sure the data associated with the notification is the correct and current one (eg. potential hash collision with pending intent request codes for 2 separate messages) and immutable flag is required by Android for security reasons I believe to make sure malicious apps can't hijack your notifications or something like that. Will also add comment before merging |
||
| ) | ||
|
|
||
| // TODO: Replace with actual app icon | ||
| val notification = NotificationCompat.Builder(this, CHANNEL_ID) | ||
| .setContentTitle(title) | ||
| .setContentText(body) | ||
| .setSmallIcon(R.drawable.ic_google) | ||
zachseidner1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .setAutoCancel(true) | ||
| .setPriority(NotificationCompat.PRIORITY_HIGH) | ||
| .setContentIntent(pendingIntent) | ||
| .build() | ||
|
|
||
| notificationManager.notify(System.currentTimeMillis().toInt(), notification) | ||
| } | ||
|
|
||
| //TODO: Add more channels for different notification types | ||
| companion object { | ||
| private const val CHANNEL_ID = "hustle_notifications_general" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.cornellappdev.hustle.data.repository | ||
|
|
||
| import com.google.firebase.messaging.FirebaseMessaging | ||
| import kotlinx.coroutines.tasks.await | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| interface FcmTokenRepository { | ||
| suspend fun getFcmToken(): Result<String> | ||
| suspend fun updateFcmToken(token: String): Result<Unit> | ||
| } | ||
|
|
||
| @Singleton | ||
| class FcmTokenRepositoryImpl @Inject constructor( | ||
| private val firebaseMessaging: FirebaseMessaging, | ||
| // TODO: Add your API service to send token to backend | ||
| ) : FcmTokenRepository { | ||
|
|
||
| override suspend fun getFcmToken(): Result<String> = runCatching { | ||
| firebaseMessaging.token.await() | ||
| } | ||
|
|
||
| override suspend fun updateFcmToken(token: String): Result<Unit> = runCatching { | ||
| // TODO: Send token to backend | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did you decide on these flags, perhaps leave a comment about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Admittedly, I didn't think too much about these flags and their functionality and mainly followed example code, but I believe the new task is to prevent crashes if the app is launched from a service in the background, and the clear top flag is to prevent duplicate instances of activities if say we minimized the app and then clicked on the notification to open it. I'll probably add a comment before merging