|
| 1 | +package com.expensify.chat.customairshipextender |
| 2 | + |
| 3 | +import android.app.NotificationChannel |
| 4 | +import android.app.NotificationManager |
| 5 | +import android.app.PendingIntent |
| 6 | +import android.content.Context |
| 7 | +import android.content.Intent |
| 8 | +import android.net.Uri |
| 9 | +import android.os.Build |
| 10 | +import androidx.core.app.NotificationCompat |
| 11 | +import com.expensify.chat.R |
| 12 | +import com.urbanairship.liveupdate.LiveUpdate |
| 13 | +import com.urbanairship.liveupdate.LiveUpdateEvent |
| 14 | +import com.urbanairship.liveupdate.LiveUpdateResult |
| 15 | +import com.urbanairship.liveupdate.SuspendLiveUpdateNotificationHandler |
| 16 | + |
| 17 | +class GpsLiveUpdateHandler : SuspendLiveUpdateNotificationHandler() { |
| 18 | + |
| 19 | + override suspend fun onUpdate( |
| 20 | + context: Context, |
| 21 | + event: LiveUpdateEvent, |
| 22 | + update: LiveUpdate |
| 23 | + ): LiveUpdateResult<NotificationCompat.Builder> { |
| 24 | + if (event == LiveUpdateEvent.END) { |
| 25 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 26 | + context.stopService(Intent(context, GpsTripService::class.java)) |
| 27 | + } |
| 28 | + return LiveUpdateResult.cancel() |
| 29 | + } |
| 30 | + |
| 31 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 32 | + context.startService(Intent(context, GpsTripService::class.java)) |
| 33 | + } |
| 34 | + ensureNotificationChannel(context) |
| 35 | + |
| 36 | + val title = update.content.opt("title").optString() ?: "GPS tracking in progress" |
| 37 | + val body = update.content.opt("body").optString() ?: "Go to the app to finish" |
| 38 | + |
| 39 | + val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) |
| 40 | + .setSmallIcon(R.drawable.ic_notification) |
| 41 | + .setContentTitle(title) |
| 42 | + .setContentText(body) |
| 43 | + .setPriority(NotificationCompat.PRIORITY_HIGH) |
| 44 | + .setCategory(NotificationCompat.CATEGORY_EVENT) |
| 45 | + .setAutoCancel(false) |
| 46 | + |
| 47 | + val deepLink = update.content.opt("deepLink").optString() |
| 48 | + if (!deepLink.isNullOrEmpty()) { |
| 49 | + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("new-expensify://$deepLink")).apply { |
| 50 | + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP |
| 51 | + } |
| 52 | + val pendingIntent = PendingIntent.getActivity( |
| 53 | + context, |
| 54 | + 0, |
| 55 | + intent, |
| 56 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE |
| 57 | + ) |
| 58 | + builder.setContentIntent(pendingIntent) |
| 59 | + } |
| 60 | + |
| 61 | + return LiveUpdateResult.ok(builder) |
| 62 | + } |
| 63 | + |
| 64 | + private fun ensureNotificationChannel(context: Context) { |
| 65 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return |
| 66 | + |
| 67 | + val manager = context.getSystemService(NotificationManager::class.java) |
| 68 | + if (manager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) != null) return |
| 69 | + |
| 70 | + val channel = NotificationChannel( |
| 71 | + NOTIFICATION_CHANNEL_ID, |
| 72 | + NOTIFICATION_CHANNEL_NAME, |
| 73 | + NotificationManager.IMPORTANCE_HIGH |
| 74 | + ).apply { |
| 75 | + description = NOTIFICATION_CHANNEL_DESCRIPTION |
| 76 | + } |
| 77 | + manager.createNotificationChannel(channel) |
| 78 | + } |
| 79 | + |
| 80 | + companion object { |
| 81 | + const val NOTIFICATION_CHANNEL_ID = "gps_trip_notification_channel_id" |
| 82 | + const val NOTIFICATION_CHANNEL_NAME = "GPS Trip Tracking" |
| 83 | + const val NOTIFICATION_CHANNEL_DESCRIPTION = "Shows when a GPS distance trip is being tracked" |
| 84 | + const val TYPE = "gps_trip_notification" |
| 85 | + } |
| 86 | +} |
0 commit comments