I built a small application that has reminders and sends notifications whenever your reminder is triggered, What I want to add is a full screen notification (or an activity that pops when the alarm is triggered) whether the user's phone is off, locked, or whatever. The same idea of the default alarm app of android phones. I searched everywhere, youtube, google, etc.. But didn't find anything
I tried to use setFullScreenIntent(), but only a normal notification showed.
Here is my notification channel code in main activity:
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
var sound = Uri.parse("android.resource://" + applicationContext.packageName + "/" + R.raw.siren);
val soundAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val name : CharSequence = "ReminderAlarm"
val description = "Channel for alarm manager"
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel("androidAlarm4", name, importance)
notificationChannel.description = description
notificationChannel.setSound(sound, soundAttributes)
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(notificationChannel)
Log.e("notification: ", "created a notification ")
}
}
my add alarm class in a AddActivity:
private fun setAlarm() {
alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, (0..19991).random(), intent, PendingIntent.FLAG_IMMUTABLE)
calendar.set(Calendar.HOUR_OF_DAY, editTime.hour)
calendar.set(Calendar.MINUTE, editTime.minute)
calendar.set(Calendar.SECOND, 0)
if (repeatSwitch.isChecked){
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
AlarmManager.INTERVAL_DAY,
pendingIntent
)
Toast.makeText(this, "Repeating alarm set", Toast.LENGTH_SHORT).show()
Log.e("notification: ", "${pendingIntent.hashCode()}")
}else {
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
pendingIntent
)
Toast.makeText(this, "alarm set", Toast.LENGTH_SHORT).show()
Log.e("notification: ", "${pendingIntent.hashCode()}")
}
}
My alarm receiver class:
private lateinit var pendingIntent: PendingIntent
private lateinit var fullScreenPendingIntent: PendingIntent
class AlarmReceiver : BroadcastReceiver() {
val notificationId = 2
val channelID = "androidAlarm4"
override fun onReceive(context: Context?, intent: Intent?) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val fullScreenNotification = Intent(context, AlarmPage::class.java)
fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenNotification,PendingIntent.FLAG_IMMUTABLE)
var sound = Uri.parse("android.resource://" + context?.packageName + "/" + R.raw.siren);
val notification = NotificationCompat.Builder(context!!, channelID)
.setSmallIcon(drawable.notification_bg)
.setContentTitle("Reminder OFF")
.setContentText("Work has to be done now!!")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(sound)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setFullScreenIntent(pendingIntent, true)
.build()
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notification)
Log.e("notification: ", "received a notification: ")
}
}
The page that should pop when the alarm is off is empty, I don't know if any code should be added their, this is the page:
class AlarmPage : AppCompatActivity() {
private lateinit var stopReminder : Button
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_alarm_page)
init()
stopReminder.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
private fun init(){
stopReminder = findViewById(R.id.stopReminder)
}
}
I have no idea what is wrong..