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

[feat] FCM 설정 및 서비스 구현 #17

Merged
merged 10 commits into from
Feb 4, 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
8 changes: 8 additions & 0 deletions .github/workflows/ktlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ jobs:

- name: Ktlint Check
run: ./gradlew --no-daemon ktlintCheck --continue

- name: Create file
run: cat /home/runner/work/Don-t-Forget---Android/Don-t-Forget---Android/app/google-services.json | base64

- name: Putting data
env:
DATA: ${{ secrets.GOOGLE_SERVICES_JSON }}
run: echo $DATA > /home/runner/work/Don-t-Forget---Android/Don-t-Forget---Android/app/google-services.json

- name: Build with Gradle
run: ./gradlew build
7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id("com.google.gms.google-services")
}

android {
Expand Down Expand Up @@ -86,4 +87,10 @@ dependencies {
implementation("androidx.datastore:datastore-preferences-core:${Versions.datastore}")
implementation("androidx.datastore:datastore-preferences:${Versions.datastore}")
implementation("io.coil-kt:coil-compose:${Versions.coil}")

implementation(platform("com.google.firebase:firebase-bom:${Versions.firebase_bom}"))
implementation("com.google.firebase:firebase-messaging")
implementation("com.google.firebase:firebase-analytics-ktx")
implementation("com.google.firebase:firebase-messaging-directboot:${Versions.fcm_direct}")
implementation("com.google.accompanist:accompanist-permissions:${Versions.permission}")
}
27 changes: 26 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".DontForgetApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/ic_app"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Dont_forget"
tools:targetApi="31">

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_app" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/white" />


<activity
android:name=".MainActivity"
android:exported="true"
Expand All @@ -25,6 +42,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="nexters.hyomk.dontforget.fcm.MyFirebaseMessageService"
android:directBootAware="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>

</manifest>
Binary file added app/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package nexters.hyomk.dontforget.fcm

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.os.Build
import androidx.compose.ui.input.key.Key.Companion.J
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import nexters.hyomk.dontforget.MainActivity
import nexters.hyomk.dontforget.R
import timber.log.Timber

class MyFirebaseMessageService : FirebaseMessagingService() {
// 새로운 토큰이 생성될 때 마다 해당 콜백이 호출된다.
override fun onNewToken(token: String) {
super.onNewToken(token)
Timber.d("onNewToken: $token")
// TODO 새로운 토큰 수신 시 서버로 전송
// MainActivity.uploadToken(token)
}

// Foreground에서 Push Service를 받기 위해 Notification 설정
override fun onMessageReceived(remoteMessage: RemoteMessage) {
J
Timber.d("From: ${remoteMessage.from}")

// Check if message contains a data payload.
if (remoteMessage.data.isNotEmpty()) {
Timber.d("Message data payload: ${remoteMessage.data}")

// Check if data needs to be processed by long running job
}

// Check if message contains a notification payload.
remoteMessage.notification?.let {
Timber.d("Message Notification Body: ${it.body}")
sendNotification(it.title ?: getString(R.string.default_notification_channel_id), it.body ?: "")
}
}

private fun sendNotification(title: String, body: String) {
val notifyId = (System.currentTimeMillis() / 7).toInt()
Timber.d("get noti$title / $body")

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent =
PendingIntent.getActivity(this, notifyId, intent, PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)

val channelId = getString(R.string.default_notification_channel_id)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_app)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationManagerCompat.IMPORTANCE_HIGH)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)

val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
channelId,
NotificationManager.IMPORTANCE_HIGH,
)
notificationManager.createNotificationChannel(channel)
}

notificationManager.notify(notifyId, notificationBuilder.build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fun AppNavHost(
startDestination = startDestination,
) {
composable(NavigationItem.Splash.route) {
SplashScreen(navController)
SplashScreen(navHostController = navController)
}
composable(NavigationItem.Home.route) { HomeScreen(navController) }
composable(NavigationItem.Create.route) { CreateScreen(modifier, navController) }
Expand Down
Loading
Loading