| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,101 @@ |
| 1 |
+/* |
|
| 2 |
+ * Copyright (c) 2023 Samson Achiaga |
|
| 3 |
+ * |
|
| 4 |
+ * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 |
+ * you may not use this file except in compliance with the License. |
|
| 6 |
+ * You may obtain a copy of the License at |
|
| 7 |
+ * |
|
| 8 |
+ * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 |
+ * |
|
| 10 |
+ * Unless required by applicable law or agreed to in writing, software |
|
| 11 |
+ * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 |
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 |
+ * See the License for the specific language governing permissions and |
|
| 14 |
+ * limitations under the License. |
|
| 15 |
+ */ |
|
| 16 |
+ |
|
| 17 |
+package com.certified.audionote.utils |
|
| 18 |
+ |
|
| 19 |
+import android.app.NotificationManager |
|
| 20 |
+import android.content.Context |
|
| 21 |
+import android.media.RingtoneManager |
|
| 22 |
+import android.os.Build |
|
| 23 |
+import androidx.core.app.NotificationCompat |
|
| 24 |
+import androidx.core.app.NotificationCompat.DEFAULT_ALL |
|
| 25 |
+import androidx.core.app.NotificationCompat.PRIORITY_HIGH |
|
| 26 |
+import androidx.core.content.res.ResourcesCompat |
|
| 27 |
+import androidx.navigation.NavDeepLinkBuilder |
|
| 28 |
+import androidx.work.Worker |
|
| 29 |
+import androidx.work.WorkerParameters |
|
| 30 |
+import com.certified.audionote.R |
|
| 31 |
+import com.certified.audionote.model.Note |
|
| 32 |
+import com.certified.audionote.ui.EditNoteFragmentArgs |
|
| 33 |
+import com.certified.audionote.ui.main.MainActivity |
|
| 34 |
+ |
|
| 35 |
+class NotificationWorker(private val appContext: Context, workerParams: WorkerParameters) : |
|
| 36 |
+ Worker(appContext, workerParams) {
|
|
| 37 |
+ override fun doWork(): Result {
|
|
| 38 |
+ |
|
| 39 |
+ val noteId = inputData.getInt("noteId", 0)
|
|
| 40 |
+ val noteTitle = inputData.getString("noteTitle")
|
|
| 41 |
+ val noteDescription = inputData.getString("noteDescription")
|
|
| 42 |
+ val noteColor = inputData.getInt("noteColor", -1)
|
|
| 43 |
+ val noteLastModificationDate = inputData.getLong("noteLastModificationDate", -1L)
|
|
| 44 |
+ val noteSize = inputData.getString("noteSize")
|
|
| 45 |
+ val noteAudioLength = inputData.getLong("noteAudioLength", 0L)
|
|
| 46 |
+ val noteFilePath = inputData.getString("noteFilePath")
|
|
| 47 |
+ val noteStarted = inputData.getBoolean("noteStarted", false)
|
|
| 48 |
+ val noteReminder = inputData.getLong("noteReminder", -1L)
|
|
| 49 |
+ |
|
| 50 |
+ val note = Note( |
|
| 51 |
+ noteTitle!!, |
|
| 52 |
+ noteDescription!!, |
|
| 53 |
+ noteColor, |
|
| 54 |
+ noteLastModificationDate, |
|
| 55 |
+ noteSize!!, |
|
| 56 |
+ noteAudioLength, |
|
| 57 |
+ noteFilePath!!, |
|
| 58 |
+ noteStarted, |
|
| 59 |
+ noteReminder, |
|
| 60 |
+ noteId, |
|
| 61 |
+ ) |
|
| 62 |
+ notifyUser(appContext, note) |
|
| 63 |
+ |
|
| 64 |
+ return Result.success() |
|
| 65 |
+ } |
|
| 66 |
+ |
|
| 67 |
+ private fun notifyUser(context: Context, note: Note) {
|
|
| 68 |
+ |
|
| 69 |
+ val defaultSoundUri = |
|
| 70 |
+ RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) |
|
| 71 |
+ val notificationManager = |
|
| 72 |
+ context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager |
|
| 73 |
+ |
|
| 74 |
+ val pendingIntent = |
|
| 75 |
+ NavDeepLinkBuilder(context) |
|
| 76 |
+ .setGraph(R.navigation.navigation) |
|
| 77 |
+ .setDestination( |
|
| 78 |
+ R.id.editNoteFragment, |
|
| 79 |
+ EditNoteFragmentArgs(note).toBundle() |
|
| 80 |
+ ) |
|
| 81 |
+ .setComponentName(MainActivity::class.java) |
|
| 82 |
+ .createPendingIntent() |
|
| 83 |
+ |
|
| 84 |
+ val notificationBuilder = |
|
| 85 |
+ NotificationCompat.Builder(context, context.getString(R.string.channelId)) |
|
| 86 |
+ .setSmallIcon(R.drawable.ic_baseline_notifications_24) |
|
| 87 |
+ .setContentTitle(note.title) |
|
| 88 |
+ .setContentText("Hi there, it's time to check out ${note.title}")
|
|
| 89 |
+ .setColor(ResourcesCompat.getColor(context.resources, R.color.primary, null)) |
|
| 90 |
+ .setSound(defaultSoundUri) |
|
| 91 |
+ .setDefaults(DEFAULT_ALL) |
|
| 92 |
+ .setContentIntent(pendingIntent) |
|
| 93 |
+ .setPriority(PRIORITY_HIGH) |
|
| 94 |
+ .setAutoCancel(true) |
|
| 95 |
+ |
|
| 96 |
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) |
|
| 97 |
+ notificationBuilder.setChannelId(context.getString(R.string.channelId)) |
|
| 98 |
+ |
|
| 99 |
+ notificationManager.notify(note.id, notificationBuilder.build()) |
|
| 100 |
+ } |
|
| 101 |
+} |
|
| 0 | 102 |
\ No newline at end of file |