Skip to content

Commit 88d98ae

Browse files
committed
Sticky Notification Removed "it's annoying"
Removed this whole entire function/snippet: private fun createNotification(): Notification { and other lines from the StickyNotificationService.kt file added line to manifest: implementation 'com.google.android.gms:play-services-auth:20.0.0' MainActivity.kt and BootReceiver.kt have had: val service = context.startForegroundService(notificationIntent) changed to val service = context.startService(notificationIntent) these changes make sure there is no permanent notification you cannot swipe away and fixed any build errors.
1 parent 302c761 commit 88d98ae

4 files changed

Lines changed: 13 additions & 50 deletions

File tree

android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ android {
4747
}
4848

4949
dependencies {
50+
implementation 'com.google.android.gms:play-services-auth:20.0.0'
5051
implementation platform('com.google.firebase:firebase-bom:30.1.0')
5152
implementation 'com.google.firebase:firebase-analytics-ktx'
5253
implementation 'com.google.firebase:firebase-messaging-ktx'

android/app/src/main/java/com/httpsms/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class MainActivity : AppCompatActivity() {
161161
return
162162
}
163163
val notificationIntent = Intent(context, StickyNotificationService::class.java)
164-
val service = context.startForegroundService(notificationIntent)
164+
val service = context.startService(notificationIntent)
165165
Timber.d("foreground service started [${service?.className}]")
166166
}
167167

android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class BootReceiver : BroadcastReceiver() {
2525

2626
Timber.d("starting foreground service")
2727
val notificationIntent = Intent(context, StickyNotificationService::class.java)
28-
val service = context.startForegroundService(notificationIntent)
28+
val service = context.startService(notificationIntent)
2929
Timber.d("foreground service started [${service?.className}]")
3030
}
3131
}
Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.httpsms.services
22

3-
import android.app.*
4-
import android.content.Context
3+
import android.app.Service
54
import android.content.Intent
65
import android.os.IBinder
7-
import android.widget.Toast
8-
import com.httpsms.MainActivity
9-
import com.httpsms.R
106
import timber.log.Timber
117

12-
class StickyNotificationService: Service() {
8+
@Suppress("DEPRECATION")
9+
class StickyNotificationService : Service() {
10+
1311
override fun onBind(intent: Intent?): IBinder? {
1412
Timber.d("Some component want to bind with the service [${intent?.action}]")
1513
return null
@@ -18,58 +16,22 @@ class StickyNotificationService: Service() {
1816
override fun onCreate() {
1917
Timber.d("The service has been created")
2018
super.onCreate()
21-
val notification = createNotification()
22-
startForeground(1, notification)
2319
}
2420

2521
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
2622
Timber.d("onStartCommand executed with startId: $startId")
27-
// by returning this we make sure the service is restarted if the system kills the service
23+
24+
25+
26+
// If we return START_STICKY, the service will be restarted if it gets terminated by the system
2827
return START_STICKY
2928
}
3029

3130
override fun onDestroy() {
3231
super.onDestroy()
3332
Timber.d("The service has been destroyed")
34-
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show()
33+
// Remove the notification when the service is destroyed
34+
stopForeground(true)
3535
}
3636

37-
38-
private fun createNotification(): Notification {
39-
val notificationChannelId = "sticky_notification_channel"
40-
41-
// depending on the Android API that we're dealing with we will have
42-
// to use a specific method to create the notification
43-
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
44-
val channel = NotificationChannel(
45-
notificationChannelId,
46-
notificationChannelId,
47-
NotificationManager.IMPORTANCE_HIGH
48-
).apply {
49-
enableVibration(false)
50-
setShowBadge(false)
51-
}
52-
notificationManager.createNotificationChannel(channel)
53-
54-
val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let {
55-
notificationIntent -> PendingIntent.getActivity(
56-
this,
57-
0,
58-
notificationIntent,
59-
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
60-
}
61-
62-
val builder: Notification.Builder = Notification.Builder(
63-
this,
64-
notificationChannelId
65-
)
66-
67-
return builder
68-
.setContentTitle("httpSMS Listener")
69-
.setContentText("httpSMS is listening for sent and received SMS messages in the background.")
70-
.setContentIntent(pendingIntent)
71-
.setOngoing(true)
72-
.setSmallIcon(R.drawable.ic_stat_name)
73-
.build()
74-
}
7537
}

0 commit comments

Comments
 (0)