@@ -4,7 +4,95 @@ import android.os.Bundle
44import io.flutter.embedding.android.FlutterActivity
55
66class MainActivity : FlutterActivity () {
7- override fun onCreate (savedInstanceState : Bundle ? ) {
8- super .onCreate(savedInstanceState)
7+ private val CHANNEL = " moe.iacg.hrpush/notification"
8+ private val NOTIFICATION_ID = 1001
9+ private val NOTIFICATION_CHANNEL_ID = " hr_push_live"
10+
11+ override fun configureFlutterEngine (flutterEngine : io.flutter.embedding.engine.FlutterEngine ) {
12+ super .configureFlutterEngine(flutterEngine)
13+ io.flutter.plugin.common.MethodChannel (flutterEngine.dartExecutor.binaryMessenger, CHANNEL ).setMethodCallHandler { call, result ->
14+ if (call.method == " updateNotification" ) {
15+ val bpm = call.argument<Int >(" bpm" )
16+ val deviceName = call.argument<String >(" deviceName" )
17+ val isConnected = call.argument<Boolean >(" isConnected" ) ? : false
18+
19+ updateOldNotification(bpm, deviceName, isConnected)
20+ result.success(null )
21+ } else if (call.method == " cancelNotification" ) {
22+ val notificationManager = getSystemService(android.content.Context .NOTIFICATION_SERVICE ) as android.app.NotificationManager
23+ notificationManager.cancel(NOTIFICATION_ID )
24+ result.success(null )
25+ } else {
26+ result.notImplemented()
27+ }
28+ }
29+ }
30+
31+ private fun updateOldNotification (bpm : Int? , deviceName : String? , isConnected : Boolean ) {
32+ android.util.Log .d(" HrPush" , " updateNotification: bpm=$bpm , connected=$isConnected " )
33+ val notificationManager = getSystemService(android.content.Context .NOTIFICATION_SERVICE ) as android.app.NotificationManager
34+
35+ // Create Channel if needed
36+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .O ) {
37+ val channel = android.app.NotificationChannel (
38+ NOTIFICATION_CHANNEL_ID ,
39+ " Live Activity" ,
40+ android.app.NotificationManager .IMPORTANCE_LOW
41+ ).apply {
42+ description = " Shows live heart rate"
43+ setSound(null , null )
44+ enableVibration(false )
45+ setShowBadge(false )
46+ lockscreenVisibility = android.app.Notification .VISIBILITY_PUBLIC
47+ }
48+ notificationManager.createNotificationChannel(channel)
49+ }
50+
51+ // Setup RemoteViews
52+ val views = android.widget.RemoteViews (packageName, R .layout.live_activity)
53+
54+ if (isConnected) {
55+ val bpmText = if (bpm != null && bpm > 0 ) " $bpm BPM" else " -- BPM"
56+ views.setTextViewText(R .id.bpm_value, bpmText)
57+ views.setTextViewText(R .id.status_text, " Connected to ${deviceName ? : " Device" } " )
58+ views.setTextViewText(R .id.time_text, " LIVE" )
59+ views.setTextColor(R .id.time_text, android.graphics.Color .parseColor(" #34C759" )) // Green
60+ } else {
61+ views.setTextViewText(R .id.bpm_value, " --" )
62+ views.setTextViewText(R .id.status_text, " Disconnected" )
63+ views.setTextViewText(R .id.time_text, " OFF" )
64+ views.setTextColor(R .id.time_text, android.graphics.Color .parseColor(" #86868B" )) // Grey
65+ }
66+
67+ // Build Notification
68+ val builder = android.app.Notification .Builder (this )
69+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .O ) {
70+ builder.setChannelId(NOTIFICATION_CHANNEL_ID )
71+ }
72+
73+ // Intent to open app
74+ val intent = android.content.Intent (this , MainActivity ::class .java)
75+ val pendingIntent = android.app.PendingIntent .getActivity(this , 0 , intent, android.app.PendingIntent .FLAG_IMMUTABLE )
76+
77+ // Use valid notification icon (white monochrome)
78+ builder.setSmallIcon(R .drawable.ic_stat_heart)
79+
80+ builder.setCustomContentView(views)
81+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .S ) {
82+ builder.setCustomBigContentView(views)
83+ // Style workaround for some Android 12+ devices to ensure custom view shows
84+ builder.setStyle(android.app.Notification .DecoratedCustomViewStyle ())
85+ }
86+
87+ builder.setContentIntent(pendingIntent)
88+ builder.setOngoing(true )
89+ builder.setOnlyAlertOnce(true )
90+ builder.setVisibility(android.app.Notification .VISIBILITY_PUBLIC )
91+
92+ try {
93+ notificationManager.notify(NOTIFICATION_ID , builder.build())
94+ } catch (e: Exception ) {
95+ android.util.Log .e(" HrPush" , " Failed to notify" , e)
96+ }
997 }
1098}
0 commit comments