+2

Android reminder notifications

Lời mở đầu

Kể từ API 19, tất cả các hàm lặp của AlarmManager đều không chính xác. Việc này tạo ra không ít khó khăn cho các lập trình viên. Sau một thời gian tìm hiểu thì mình đã quyết định dùng setExact để thực hiện gửi thông báo 1 lần. Trong hàm nhận thông báo mình sẽ sử dụng dữ liệu trong room database để cài đặt cho lần thông báo tiếp theo.

1. Cấp quyền

  • Chúng ta cần quyền SCHEDULE_EXACT_ALARM để hàm AlarmManager.setExact có thể hoạt động.
  • SCHEDULE_EXACT_ALARM sẽ tự động được cấp đối với API <= 31
  • Đối với API > 31 chúng ta cần kiểm tra và xin cấp quyền nếu chưa được cho phép.
  • Kiểm tra quyền:
ContextCompat.checkSelfPermission(context, Manifest.permission.SCHEDULE_EXACT_ALARM)

Yêu cầu quyền:

val launcher = rememberLauncherForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) {
        // TODO
    }
    
launcher.launch(Manifest.permission.SCHEDULE_EXACT_ALARM)

2. Tạo ReminderBroadcastReceiver

  • Chúng ta sử dụng BroadcastReceiver để nhận sự kiện và hiển thị Notification cho user
  • Sử dụng room để kiểm tra và thực hiển setExact Alarm cho lần tiếp theo
@AndroidEntryPoint
class ReminderBroadcastReceiver : BroadcastReceiver() {

    @Inject
    lateinit var reminderRepository: ReminderRepository

    companion object {
        const val CONTENT_TEXT = "CONTENT_TEXT"
        const val REMINDER_ID = "REMINDER_ID"
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        val ctx = context ?: return
        val notificationManager =
            ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (!notificationManager.areNotificationsEnabled()) return

        val reminderId = intent?.getIntExtra(REMINDER_ID, -1) ?: -1
        if (reminderId != -1) {
            CoroutineScope(Dispatchers.IO).launch {
                reminderRepository.checkToResetReminder(reminderId)
            }
        }

        val notification = NotificationCompat.Builder(ctx, Constants.REMINDER_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(ctx.getString(R.string.app_name))
            .setContentText(intent?.getStringExtra(CONTENT_TEXT))
            .build()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                Constants.REMINDER_CHANNEL_ID,
                Constants.REMINDER_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = Constants.REMINDER_CHANNEL_DESC
            }
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(Random.nextInt(), notification)
    }
}
  • Nếu reminder trong room chưa bị xóa thì thực hiện set cho lần tiếp theo
override suspend fun checkToResetReminder(id: Int) {
        try {
            val reminder = getReminderById(id.toLong())
             reminderManager.scheduleReminder(
                    content = reminder.label,
                    time = reminder.reminderTime,
                    reminderId = reminder.id.toInt()
                )
        } catch (_: Exception) {
        }
    }

Thêm vào manifest file

<receiver
            android:name=".ui.reminder_editing.ReminderBroadcastReceiver"
            android:enabled="true" />

3. Tạo class để quản lý việc thêm và hủy Notification

  • Ở đây mình dùng requestCode trong pendingIntent bằng với Id của notification trong room để tiện cho việc hủy Notification
class ReminderManager(
    private val context: Context
) {
    fun scheduleReminder(content: String, time: LocalTime, reminderId: Int) {
        val intent = Intent(context, ReminderBroadcastReceiver::class.java)
        intent.putExtra(ReminderBroadcastReceiver.CONTENT_TEXT, content)
        intent.putExtra(ReminderBroadcastReceiver.REMINDER_ID, reminderId)
        val pendingIntent = PendingIntent.getBroadcast(
            context,
            reminderId,
            intent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
        val notificationTime = Calendar.getInstance().apply {
            set(Calendar.HOUR_OF_DAY, time.hour)
            set(Calendar.MINUTE, time.minute)
            set(Calendar.SECOND, 0)
        }

        // Do not trigger reminder in the pass
        if (Calendar.getInstance().apply {
                add(Calendar.SECOND, 30)
            }.timeInMillis - notificationTime.timeInMillis > 0) {
            notificationTime.add(Calendar.DATE, 1)
        }

        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmManager.setExact(
            AlarmManager.RTC_WAKEUP,
            notificationTime.timeInMillis,
            pendingIntent
        )
    }

    fun cancelReminder(reminderId: Int) {
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent(context, ReminderBroadcastReceiver::class.java)
        val pendingIntent = PendingIntent.getBroadcast(
            context,
            reminderId,
            intent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
        alarmManager.cancel(pendingIntent)
    }
}

4. Gọi hàm hiển thị Notification

  • Thêm Notification vào trong Room Database.
  • Sử dụng Id trả về để đặt cho notificationId để tiện cho việc sửa và hủy Notification
@Entity(tableName = TABLE_REMINDER)
data class Reminder constructor(
    @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Long = 0,
    @ColumnInfo("label") val label: String,
    @ColumnInfo("activated") val activated: Boolean,
    @ColumnInfo("color") val color: Long,
    @ColumnInfo("reminder_time") val reminderTime: LocalTime,
    @ColumnInfo("id_user") val idUser: String? = null
)

@Dao
interface ReminderDao {
    @Query("SELECT * FROM ${Constants.TABLE_REMINDER}")
    fun getReminderList(): Flow<List<Reminder>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun addReminder(reminder: Reminder): Long // NotificationId

    @Query("SELECT * FROM ${Constants.TABLE_REMINDER} WHERE id=:id")
    suspend fun getReminder(id: Long): Reminder

    @Query("DELETE FROM ${Constants.TABLE_REMINDER} WHERE id=:id")
    suspend fun deleteReminder(id: Long)
}
  • Trigger
val reminderManager = ReminderManager(context);
reminderManager.scheduleReminder("Hello!", LocalTime(8, 0), NotificationId);
  1. Xử lý reboot
  • Sau khi điện thoại được khởi động lại. Các hàm trong AlarmManager sẽ được reset. Do đó chúng ta cần bắt sự kiện reboot và đặt lại reminder để hiển thị Notification. Ở đây mình sẽ sử dụng broadcast receiver
class BootReceiver : BroadcastReceiver() {

    override fun onReceive(p0: Context?, p1: Intent?) {
        if (p1?.action == "android.intent.action.BOOT_COMPLETED") {
            // Đặt lại Notification ở đây, dựa vào dữ liệu được lưu trữ trong room
        }
    }
}
  • Khai báo trong manifest
<receiver
            android:name=".ui.reminder_editing.BootReceiver"
            android:enabled="true"
            android:exported="false">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí