Huy Thao
Huy Thao

Reputation: 11

Notification full screen on wear os

I want to show a notification on the watch when the watch locks the screen

I tried by creating a service with an alarm, then listening at the receiver to display a notification. Here, I have set the properties height, category alarm, visibility public, full screen intent, but the notification still does not display when the watch locks the screen, it only sound and ring. I have added permission USE_FULL_SCREEN_INTENT, POST_NOTIFICATIONS Here is my code MainActivity:

val intent = Intent(this, NotificationService::class.java)
        this.startService(intent);

NotificationService:

private fun startNotification() {
        val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent(this, AlarmReceiver::class.java)
        val pendingIntent = PendingIntent.getBroadcast(
            this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE or 0
        )

        // Set the alarm to start at a specific time
        val calendar: Calendar = Calendar.getInstance().apply {
            timeInMillis = System.currentTimeMillis()
            set(Calendar.HOUR_OF_DAY, 10)
            set(Calendar.MINUTE, 45)
        }

        // setExactAndAllowWhileIdle() schedules an alarm to trigger at the exact time, even in doze mode
        alarmManager.setExactAndAllowWhileIdle(
            AlarmManager.RTC_WAKEUP,
            calendar.timeInMillis,
            pendingIntent
        )
    }

AlarmReceiver:

override fun onReceive(context: Context, intent: Intent) {
        // This will be called when the alarm goes off
        // You can start an activity or a service here
        Log.d("RECEIVER", "ALARM");
        val intent = Intent(context, CancelActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val dismissIntent = Intent(context, CancelNotificationReceiver::class.java)
        val pendingIntent: PendingIntent = PendingIntent.getActivity(
            context,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
        )
        val dismissIntentPending = PendingIntent.getBroadcast(
            context,
            0,
            dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
        )

        val notification = NotificationCompat.Builder(context, "alarm_channel")
            .setSmallIcon(androidx.core.R.drawable.ic_call_answer)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .addAction(androidx.core.R.drawable.ic_call_answer, "救助要請", pendingIntent)
            .addAction(androidx.core.R.drawable.ic_call_decline, "キャンセル", dismissIntentPending)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setVibrate(longArrayOf(0, 1000, 500, 1000)).setFullScreenIntent(pendingIntent, true)
            .build()

        if (ActivityCompat.checkSelfPermission(
                context, Manifest.permission.POST_NOTIFICATIONS
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            return
        }
        with(NotificationManagerCompat.from(context)) {
            notify(125, notification)
        }
    }

Upvotes: 1

Views: 178

Answers (0)

Related Questions