Skip to content

Commit e09978f

Browse files
committed
[ADD] notifications
1 parent 8eb2704 commit e09978f

File tree

6 files changed

+81
-9
lines changed

6 files changed

+81
-9
lines changed

fcm/app/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ android {
3535
dependencies {
3636

3737
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
38-
implementation 'androidx.core:core-ktx:1.3.1'
39-
implementation 'androidx.appcompat:appcompat:1.2.0'
40-
implementation 'com.google.android.material:material:1.2.1'
41-
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
38+
implementation 'androidx.core:core-ktx:1.5.0'
39+
implementation 'androidx.appcompat:appcompat:1.3.0'
40+
implementation 'com.google.android.material:material:1.3.0'
41+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
4242

4343
// firebase
4444
implementation platform('com.google.firebase:firebase-bom:28.1.0')
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.ranzed.fcm
22

33
import android.app.Application
4-
import android.util.Log
4+
import com.ranzed.fcm.notification.NotificationService
55

66
class App : Application() {
77

88
override fun onCreate() {
99
super.onCreate()
10+
NotificationService.initialize(this)
1011
}
1112

12-
1313
}

fcm/app/src/main/java/com/ranzed/fcm/activity/MessageActivity.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package com.ranzed.fcm.activity
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5-
import android.widget.TextView
5+
import android.view.View
6+
import androidx.appcompat.widget.AppCompatButton
67
import com.ranzed.fcm.R
8+
import com.ranzed.fcm.notification.NotificationService
79

810
class MessageActivity : AppCompatActivity() {
911

10-
private var helloText : TextView? = null
12+
private var createNotification : AppCompatButton? = null
1113

1214
override fun onCreate(savedInstanceState: Bundle?) {
1315
super.onCreate(savedInstanceState)
1416
setContentView(R.layout.activity_message)
15-
helloText = findViewById(R.id.hello)
17+
createNotification = findViewById(R.id.btn_create_notification)
18+
createNotification?.setOnClickListener { view : View ->
19+
NotificationService.createNotification("123", "456", this)}
1620
}
1721
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.ranzed.fcm.notification
2+
3+
import android.app.NotificationChannel
4+
import android.app.NotificationManager
5+
import android.content.Context
6+
import android.os.Build
7+
import androidx.core.app.NotificationCompat
8+
import com.ranzed.fcm.R
9+
10+
object NotificationService {
11+
12+
private val CHANNEL_ID = "fcm_channel_id"
13+
private var initialized = false
14+
15+
fun createNotification(title: String, message : String, ctx : Context) {
16+
if (!initialized)
17+
return
18+
19+
val builder = NotificationCompat.Builder(ctx, CHANNEL_ID)
20+
.setSmallIcon(R.mipmap.ic_launcher)
21+
.setContentTitle(title)
22+
.setContentText(message)
23+
24+
val notificationManager = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
25+
notificationManager.notify(0, builder.build())
26+
27+
}
28+
29+
fun initialize(ctx : Context) {
30+
if (initialized)
31+
return
32+
33+
createNotificationChannel(ctx)
34+
initialized = true
35+
}
36+
37+
private fun createNotificationChannel(ctx : Context) {
38+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
39+
return
40+
41+
val notificationManager = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
42+
43+
val c = notificationManager.getNotificationChannel(CHANNEL_ID)
44+
if (c != null)
45+
return
46+
47+
val channel = NotificationChannel(CHANNEL_ID,
48+
ctx.getString(R.string.notification_channel_name),
49+
NotificationManager.IMPORTANCE_DEFAULT)
50+
channel.description = ctx.getString(R.string.notification_channel_description)
51+
52+
notificationManager.createNotificationChannel(channel)
53+
}
54+
55+
}

fcm/app/src/main/res/layout/activity_message.xml

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
android:layout_height="match_parent"
77
tools:context=".activity.MessageActivity">
88

9+
<Button
10+
android:id="@+id/btn_create_notification"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
app:layout_constraintBottom_toBottomOf="parent"
14+
app:layout_constraintEnd_toEndOf="parent"
15+
app:layout_constraintStart_toStartOf="parent"
16+
app:layout_constraintTop_toTopOf="parent" />
17+
918
<TextView
1019
android:id="@+id/hello"
1120
android:layout_width="wrap_content"
+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<resources>
22
<string name="app_name">Cloud Messaging</string>
33
<string name="hello_message">Hello world</string>
4+
5+
<string name="notification_channel_name">Fcm channel</string>
6+
<string name="notification_channel_description">Fcm channel description</string>
7+
48
</resources>

0 commit comments

Comments
 (0)