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
+ }
0 commit comments