diff --git a/android/app/build.gradle b/android/app/build.gradle index 416e01d2..8d89072b 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -47,6 +47,7 @@ android { } dependencies { + implementation 'com.google.android.gms:play-services-auth:20.0.0' implementation platform('com.google.firebase:firebase-bom:30.1.0') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'com.google.firebase:firebase-messaging-ktx' diff --git a/android/app/src/main/java/com/httpsms/MainActivity.kt b/android/app/src/main/java/com/httpsms/MainActivity.kt index 23b59144..84b4b36d 100644 --- a/android/app/src/main/java/com/httpsms/MainActivity.kt +++ b/android/app/src/main/java/com/httpsms/MainActivity.kt @@ -161,7 +161,7 @@ class MainActivity : AppCompatActivity() { return } val notificationIntent = Intent(context, StickyNotificationService::class.java) - val service = context.startForegroundService(notificationIntent) + val service = context.startService(notificationIntent) Timber.d("foreground service started [${service?.className}]") } diff --git a/android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt b/android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt index 299c3f24..7daa0042 100644 --- a/android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt +++ b/android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt @@ -25,7 +25,7 @@ class BootReceiver : BroadcastReceiver() { Timber.d("starting foreground service") val notificationIntent = Intent(context, StickyNotificationService::class.java) - val service = context.startForegroundService(notificationIntent) + val service = context.startService(notificationIntent) Timber.d("foreground service started [${service?.className}]") } } diff --git a/android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt b/android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt index a2be58a3..adff5d29 100644 --- a/android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt +++ b/android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt @@ -1,15 +1,13 @@ package com.httpsms.services -import android.app.* -import android.content.Context +import android.app.Service import android.content.Intent import android.os.IBinder -import android.widget.Toast -import com.httpsms.MainActivity -import com.httpsms.R import timber.log.Timber -class StickyNotificationService: Service() { +@Suppress("DEPRECATION") +class StickyNotificationService : Service() { + override fun onBind(intent: Intent?): IBinder? { Timber.d("Some component want to bind with the service [${intent?.action}]") return null @@ -18,58 +16,22 @@ class StickyNotificationService: Service() { override fun onCreate() { Timber.d("The service has been created") super.onCreate() - val notification = createNotification() - startForeground(1, notification) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Timber.d("onStartCommand executed with startId: $startId") - // by returning this we make sure the service is restarted if the system kills the service + + + + // If we return START_STICKY, the service will be restarted if it gets terminated by the system return START_STICKY } override fun onDestroy() { super.onDestroy() Timber.d("The service has been destroyed") - Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show() + // Remove the notification when the service is destroyed + stopForeground(true) } - - private fun createNotification(): Notification { - val notificationChannelId = "sticky_notification_channel" - - // depending on the Android API that we're dealing with we will have - // to use a specific method to create the notification - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - val channel = NotificationChannel( - notificationChannelId, - notificationChannelId, - NotificationManager.IMPORTANCE_HIGH - ).apply { - enableVibration(false) - setShowBadge(false) - } - notificationManager.createNotificationChannel(channel) - - val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { - notificationIntent -> PendingIntent.getActivity( - this, - 0, - notificationIntent, - PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) - } - - val builder: Notification.Builder = Notification.Builder( - this, - notificationChannelId - ) - - return builder - .setContentTitle("httpSMS Listener") - .setContentText("httpSMS is listening for sent and received SMS messages in the background.") - .setContentIntent(pendingIntent) - .setOngoing(true) - .setSmallIcon(R.drawable.ic_stat_name) - .build() - } }