-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#195 [FEAT] : Firebase Cloud Message 설정 및 구현
- Loading branch information
Showing
8 changed files
with
227 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
app/src/main/java/com/example/charo_android/MyFirebaseMessagingService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters