Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Add sticky notification service

Acho Arnold0b0a66949842
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 79718343..69313f65 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -12,7 +12,9 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
-
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
+ <uses-permission android:name="android.permission.WAKE_LOCK" />
+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
@@ -38,6 +40,12 @@
<activity android:name=".LoginActivity" android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity" />
+ <service
+ android:name=".services.StickyNotificationService"
+ android:enabled="true"
+ android:exported="false">
+ </service>
+
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
@@ -53,6 +61,14 @@
</intent-filter>
</receiver>
+ <receiver android:enabled="true" android:name=".receivers.BootReceiver"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="android.intent.action.BOOT_COMPLETED"/>
+ </intent-filter>
+ </receiver>
+
+
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/notification_channel_default" />
diff --git a/android/app/src/main/java/com/httpsms/MainActivity.kt b/android/app/src/main/java/com/httpsms/MainActivity.kt
index 2ed874c0..9c493878 100644
--- a/android/app/src/main/java/com/httpsms/MainActivity.kt
+++ b/android/app/src/main/java/com/httpsms/MainActivity.kt
@@ -18,6 +18,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.google.android.material.button.MaterialButton
import com.google.android.material.switchmaterial.SwitchMaterial
+import com.httpsms.services.StickyNotificationService
import timber.log.Timber
import java.util.*
@@ -43,6 +44,8 @@ class MainActivity : AppCompatActivity() {
setActiveStatus(this)
registerListeners()
refreshToken(this)
+
+ startStickyNotification(this)
}
override fun onResume() {
@@ -52,6 +55,13 @@ class MainActivity : AppCompatActivity() {
refreshToken(this)
}
+ private fun startStickyNotification(context: Context) {
+ Timber.d("starting foreground service")
+ val notificationIntent = Intent(context, StickyNotificationService::class.java)
+ val service = context.startForegroundService(notificationIntent)
+ Timber.d("foreground service started [${service?.className}]")
+ }
+
private fun refreshToken(context: Context) {
if(!Settings.isLoggedIn(context)) {
Timber.w("cannot refresh token because owner is not logged in")
diff --git a/android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt b/android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt
new file mode 100644
index 00000000..eed7821b
--- /dev/null
+++ b/android/app/src/main/java/com/httpsms/receivers/BootReceiver.kt
@@ -0,0 +1,20 @@
+package com.httpsms.receivers
+
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+import com.httpsms.services.StickyNotificationService
+import timber.log.Timber
+
+
+class BootReceiver : BroadcastReceiver() {
+ override fun onReceive(context: Context, intent: Intent) {
+ if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
+ Intent(context, StickyNotificationService::class.java).also {
+ context.startForegroundService(it)
+ }
+ } else {
+ Timber.e("invalid intent [${intent.action}]")
+ }
+ }
+}
diff --git a/android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt b/android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt
new file mode 100644
index 00000000..c8f91c11
--- /dev/null
+++ b/android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt
@@ -0,0 +1,78 @@
+package com.httpsms.services
+
+import android.app.*
+import android.content.Context
+import android.content.Intent
+import android.graphics.Color
+import android.os.IBinder
+import android.widget.Toast
+import com.httpsms.MainActivity
+import com.httpsms.R
+import timber.log.Timber
+
+class StickyNotificationService: Service() {
+ override fun onBind(intent: Intent?): IBinder? {
+ Timber.d("Some component want to bind with the service [${intent?.action}]")
+ return null
+ }
+
+ 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
+ return START_STICKY
+ }
+
+ override fun onDestroy() {
+ super.onDestroy()
+ Timber.d("The service has been destroyed")
+ Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show()
+ }
+
+
+ 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
+ ).let {
+ it.enableLights(true)
+ it.enableVibration(false)
+ it.lightColor = Color.RED
+ it
+ }
+ 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("HTTP SMS")
+ .setContentText("Service running in background")
+ .setContentIntent(pendingIntent)
+ .setSmallIcon(R.mipmap.ic_launcher)
+ .setTicker("Ticker text")
+ .build()
+ }
+}