diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 6ceb8c07..cc3f5468 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -79,6 +79,9 @@ dependencies { implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.2") implementation("com.google.firebase:firebase-common-ktx:20.0.0") implementation("com.google.firebase:firebase-auth-ktx:21.0.1") + implementation("com.google.firebase:firebase-bom:29.1.0") +// implementation("com.google.firebase:firebase-analytics-ktx") + implementation("com.google.firebase:firebase-messaging:22.0.0") kapt(KaptDependencies.hilt) // DataStore diff --git a/app/google-services.json b/app/google-services.json index 08b86450..ef92e7bb 100644 --- a/app/google-services.json +++ b/app/google-services.json @@ -1,33 +1,62 @@ { "project_info": { - "project_number": "570607218249", - "project_id": "charo-c453f", - "storage_bucket": "charo-c453f.appspot.com" + "project_number": "273821551567", + "project_id": "charo-android", + "storage_bucket": "charo-android.appspot.com" }, "client": [ { "client_info": { - "mobilesdk_app_id": "1:570607218249:android:28f17d5a480df79742d998", + "mobilesdk_app_id": "1:273821551567:android:71081779a86829e57660ba", + "android_client_info": { + "package_name": "com.example.charo_android" + } + }, + "oauth_client": [ + { + "client_id": "273821551567-mu7ln5scos0nuffo78v50f161p70g66i.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyBhg8XQiG_v3HUQTkv2N679CygRxcBb8UM" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "273821551567-mu7ln5scos0nuffo78v50f161p70g66i.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:273821551567:android:a3ce90298ee43dea7660ba", "android_client_info": { "package_name": "com.hjinhee.android" } }, "oauth_client": [ { - "client_id": "570607218249-6osutorq2vh0q71gvcus4e6n6c5d4n5j.apps.googleusercontent.com", + "client_id": "273821551567-mu7ln5scos0nuffo78v50f161p70g66i.apps.googleusercontent.com", "client_type": 3 } ], "api_key": [ { - "current_key": "AIzaSyAd08-9nqjDdEj6Bx-DTB0dxAPvch3lDdE" + "current_key": "AIzaSyBhg8XQiG_v3HUQTkv2N679CygRxcBb8UM" } ], "services": { "appinvite_service": { "other_platform_oauth_client": [ { - "client_id": "570607218249-6osutorq2vh0q71gvcus4e6n6c5d4n5j.apps.googleusercontent.com", + "client_id": "273821551567-mu7ln5scos0nuffo78v50f161p70g66i.apps.googleusercontent.com", "client_type": 3 } ] diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c77f5c18..814a4ce1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,15 @@ android:theme="@style/Theme.ChaRoAndroid" android:usesCleartextTraffic="true" tools:replace="android:appComponentFactory"> + + + + + + diff --git a/app/src/main/java/com/example/charo_android/MyFirebaseMessagingService.kt b/app/src/main/java/com/example/charo_android/MyFirebaseMessagingService.kt new file mode 100644 index 00000000..a5233f0d --- /dev/null +++ b/app/src/main/java/com/example/charo_android/MyFirebaseMessagingService.kt @@ -0,0 +1,110 @@ +package com.example.charo_android + +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 android.util.Log +import androidx.core.app.NotificationCompat +import com.example.charo_android.presentation.ui.alarm.AlarmActivity +import com.google.firebase.messaging.FirebaseMessagingService +import com.google.firebase.messaging.RemoteMessage + +class MyFirebaseMessagingService : FirebaseMessagingService() { + private val TAG = "MyFirebaseMessagingService" + + override fun onNewToken(token: String) { + Log.d(TAG, "new Token: $token") + sendRegistrationToServer(token) + } + + private fun sendRegistrationToServer(token : String) { + + } + + override fun onMessageReceived(remoteMessage: RemoteMessage) { + if (remoteMessage != null) { + Log.d(TAG, "From: " + remoteMessage.data) + Log.i("notice 바디: ", remoteMessage.notification?.title.toString()) + Log.i("notice 타이틀: ", remoteMessage.notification?.body.toString()) + sendMainNotification(remoteMessage) + sendNotification(remoteMessage) + } else { + Log.i("notice 수신에러: ", "data가 비어있습니다. 메시지를 수신하지 못했습니다.") + Log.i("notice data 값: ", remoteMessage.data.toString()) + } + } + + private fun sendMainNotification(remoteMessage: RemoteMessage) { + val uniId = remoteMessage.sentTime.toInt() + + val intent = Intent(this, AlarmActivity::class.java) + intent.putExtra("isOpenFromPushAlarm", true) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + + val pendingIntent = PendingIntent.getActivity( + this, uniId, intent, PendingIntent.FLAG_ONE_SHOT + ) + + val channelId = "Notification Main Message" + + val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) + + val notificationBuilder = + NotificationCompat.Builder(this, channelId) + .setSmallIcon(R.drawable.ic_main_logo_blue) + .setContentTitle(remoteMessage.notification?.title.toString()) + .setContentText(remoteMessage.notification?.body.toString()) + .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, "Notice", NotificationManager.IMPORTANCE_DEFAULT) + notificationManager.createNotificationChannel(channel) + } + notificationManager.notify(uniId, notificationBuilder.build()) + } + + + private fun sendNotification(remoteMessage: RemoteMessage) { + val uniId = remoteMessage.sentTime.toInt() + + val intent = Intent(this, MyFirebaseMessagingService::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + + val pendingIntent = PendingIntent.getActivity( + this, uniId, intent, PendingIntent.FLAG_ONE_SHOT + ) + + val channelId = "Notification Message" + + val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) + + val notificationBuilder = + NotificationCompat.Builder(this, channelId) + .setSmallIcon(R.drawable.ic_main_logo_blue) + .setContentTitle(remoteMessage.notification?.title.toString()) + .setContentText(remoteMessage.notification?.body.toString()) + .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, "Notice", NotificationManager.IMPORTANCE_DEFAULT) + notificationManager.createNotificationChannel(channel) + } + notificationManager.notify(uniId, notificationBuilder.build()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/charo_android/presentation/ui/alarm/AlarmActivity.kt b/app/src/main/java/com/example/charo_android/presentation/ui/alarm/AlarmActivity.kt index fd9689a7..946901ee 100644 --- a/app/src/main/java/com/example/charo_android/presentation/ui/alarm/AlarmActivity.kt +++ b/app/src/main/java/com/example/charo_android/presentation/ui/alarm/AlarmActivity.kt @@ -1,9 +1,16 @@ package com.example.charo_android.presentation.ui.alarm +import android.app.NotificationChannel +import android.app.NotificationManager +import android.graphics.Color +import android.os.Build import android.os.Bundle +import android.os.Handler import android.util.Log import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.NotificationCompat import androidx.recyclerview.widget.ItemTouchHelper +import com.example.charo_android.R import com.example.charo_android.data.api.ApiService import com.example.charo_android.data.model.response.alarm.ResponseAlarmDeleteData import com.example.charo_android.data.model.response.alarm.ResponseAlarmListData @@ -24,6 +31,10 @@ class AlarmActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = ActivityAlarmBinding.inflate(layoutInflater) setContentView(binding.root) + + //fcm test + sendFCM() + goHome() alarmViewModel = AlarmViewModel() @@ -184,6 +195,41 @@ class AlarmActivity : AppCompatActivity() { }) } + private fun sendFCM(){ + var NOTIFICATION_CHANNEL_ID = "0000" + var NOTIFICATION_CHANNEL_NAME = "ChaRo-Android" + var notificationManager = getSystemService(NotificationManager::class.java) as NotificationManager? + + val handler = Handler() + handler.postDelayed(object : Runnable { + override fun run() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val importance = NotificationManager.IMPORTANCE_LOW + val notificationChannel = NotificationChannel( + NOTIFICATION_CHANNEL_ID, + NOTIFICATION_CHANNEL_NAME, + importance + ) + notificationChannel.enableLights(true) + notificationChannel.lightColor = Color.RED + notificationChannel.enableVibration(true) + notificationChannel.vibrationPattern = + longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400) + notificationManager?.createNotificationChannel(notificationChannel) + } + + val mNotificationBuilder: NotificationCompat.Builder = + NotificationCompat.Builder(applicationContext,NOTIFICATION_CHANNEL_ID) + .setSmallIcon(R.drawable.ic_main_logo_blue) + .setContentTitle("ChaRo") + .setContentText("새로운 드라이브 코스를 확인하세요!") + // .setAutoCancel(false) + + notificationManager!!.notify(0, mNotificationBuilder.build()) + } + }, 0) + } + private fun goHome() { binding.imgBackHomeAlarm.setOnClickListener { onBackPressed() diff --git a/app/src/main/java/com/example/charo_android/presentation/ui/main/MainActivity.kt b/app/src/main/java/com/example/charo_android/presentation/ui/main/MainActivity.kt index 3462d4ee..a6721ea3 100644 --- a/app/src/main/java/com/example/charo_android/presentation/ui/main/MainActivity.kt +++ b/app/src/main/java/com/example/charo_android/presentation/ui/main/MainActivity.kt @@ -21,6 +21,8 @@ import com.example.charo_android.presentation.ui.write.WriteShareActivity import com.example.charo_android.presentation.util.LoginUtil import com.example.charo_android.presentation.util.SharedInformation import com.example.charo_android.presentation.util.replaceFragment +import com.google.android.gms.tasks.OnCompleteListener +import com.google.firebase.messaging.FirebaseMessaging import org.koin.androidx.viewmodel.ext.android.viewModel class MainActivity : AppCompatActivity() { @@ -70,6 +72,7 @@ class MainActivity : AppCompatActivity() { initNavView() lookFor() + initFirebase() } @@ -211,4 +214,21 @@ class MainActivity : AppCompatActivity() { } } } + + private fun initFirebase(){ + FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> + if (!task.isSuccessful) { + Log.w( + "FirebaseTAG", + "Fetching FCM registration token failed", + task.exception + ) + return@OnCompleteListener + } else { + val token = task.result + val msg = getString(R.string.msg_token_fmt, token) + Log.d("Firebase Success", msg) + } + }) + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 523cfd87..30a9da22 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -202,5 +202,6 @@ 수정하기 삭제하기 선택안함 + fcm token: %s \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 95a421f3..6daf765b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,17 +6,15 @@ buildscript { repositories { google() mavenCentral() - - } + dependencies { classpath("com.android.tools.build:gradle:7.0.4") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10") // classpath(ClassPathDependencies.hilt) classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin_version}") classpath(ClassPathDependencies.hilt) - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files + classpath("com.google.gms:google-services:4.3.10") } }