live activity handler

live_activities
tahaalam 2 months ago
parent d232cead63
commit 841f740cbd

@ -1,15 +1,18 @@
package com.ejada.hmg.live_activity package com.ejada.hmg.live_activity
import android.app.Activity
import android.app.Notification import android.app.Notification
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content. Intent import android.content. Intent
import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import android.widget.RemoteViews import android.widget.RemoteViews
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
@ -36,6 +39,9 @@ class CustomLiveActivityManager(context: Context) :
) )
private suspend fun updateRemoteViews(data: Map<String, Any>) { private suspend fun updateRemoteViews(data: Map<String, Any>) {
Log.d("updateRemoteViews", "current ticket is ${data["queueNo"] as? String ?: ""}")
Log.d("updateRemoteViews", "remoteViews is $remoteViews")
val (nextStepButtonBackground, textColor, containerBackground) = when (data["callType"] as? Int) { val (nextStepButtonBackground, textColor, containerBackground) = when (data["callType"] as? Int) {
0 -> Triple(R.drawable.grey_rounded_corner_widget, R.color.text_color, R.drawable.whitebg_yellow_border_rounded_corner_widget) 0 -> Triple(R.drawable.grey_rounded_corner_widget, R.color.text_color, R.drawable.whitebg_yellow_border_rounded_corner_widget)
1 -> Triple(R.drawable.dark_red_rounded_corner_widget, R.color.colorWhite, R.drawable.whitebg_red_border_rounded_corner_widget) 1 -> Triple(R.drawable.dark_red_rounded_corner_widget, R.color.colorWhite, R.drawable.whitebg_red_border_rounded_corner_widget)
@ -43,17 +49,24 @@ class CustomLiveActivityManager(context: Context) :
else -> Triple(R. drawable.grey_rounded_corner_widget, R.color.text_color, R.drawable.whitebg_yellow_border_rounded_corner_widget) else -> Triple(R. drawable.grey_rounded_corner_widget, R.color.text_color, R.drawable.whitebg_yellow_border_rounded_corner_widget)
} }
var currentServingBackground = when (data["currentlyServingCallType"] as? Int) {
1->R.drawable.red_rounded_corner_widget
else ->R.drawable.green_rounded_corner_widget
}
remoteViews.setInt(R.id.nextStep, "setBackgroundResource", nextStepButtonBackground) remoteViews.setInt(R.id.nextStep, "setBackgroundResource", nextStepButtonBackground)
remoteViews.setInt(R.id.status, "setBackgroundResource", currentServingBackground)
remoteViews.setInt(R.id.container, "setBackgroundResource", containerBackground) remoteViews.setInt(R.id.container, "setBackgroundResource", containerBackground)
remoteViews.setTextColor(R.id.nextStep, ContextCompat.getColor(context, textColor)) remoteViews.setTextColor(R.id.nextStep, ContextCompat.getColor(context, textColor))
remoteViews.setTextViewText(R.id.countvalue, data["queueNo"] as? String ?: "") remoteViews.setTextViewText(R.id.countvalue, data["queueNo"] as? String ?: "")
remoteViews.setTextViewText(R.id.status, data["currentlyServingStatus"] as? String ?: "") remoteViews.setTextViewText(R.id.status, data["currentlyServingStatus"] as? String ?: "")
remoteViews.setTextViewText(R. id.currentlyserving, data["currentlyServing"] as? String ?: "") remoteViews.setTextViewText(R. id.currentlyserving, data["currentlyServingTitle"] as? String ?: "")
remoteViews.setTextViewText(R.id.nextStep, data["nextTicketStatus"] as? String ?: "") remoteViews.setTextViewText(R.id.nextStep, data["nextTicketStatus"] as? String ?: "")
} }
private suspend fun updateCollapsedView(data: Map<String, Any>) { private suspend fun updateCollapsedView(data: Map<String, Any>) {
Log.d("updateCollapsedView", "current ticket is ${data["currentTicket"] as? String ?: ""}") Log.d("updateCollapsedView", "current ticket is ${data["queueNo"] as? String ?: ""}")
Log.d("updateCollapsedView", "remoteViews is $collapesView")
val containerBackground = when (data["callType"] as? Int) { val containerBackground = when (data["callType"] as? Int) {
0 -> R.drawable.whitebg_yellow_border_rounded_corner_widget 0 -> R.drawable.whitebg_yellow_border_rounded_corner_widget
@ -70,11 +83,12 @@ class CustomLiveActivityManager(context: Context) :
event: String, event: String,
data: Map<String, Any> data: Map<String, Any>
): Notification { ): Notification {
val notificationManager = // val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager // context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (notificationManager != null) { // if (notificationManager != null) {
notificationManager.cancelAll(); // notificationManager.cancelAll();
} // }
collapesView.apply { collapesView.apply {
updateCollapsedView(data) updateCollapsedView(data)
} }
@ -86,15 +100,14 @@ class CustomLiveActivityManager(context: Context) :
.setSmallIcon(R.drawable.hmg) .setSmallIcon(R.drawable.hmg)
.setContentTitle(data["title"] as? String ?: "Patient Queue") .setContentTitle(data["title"] as? String ?: "Patient Queue")
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setContentText(data["content"] as? String ?: "Patient Queue") .setContentText(data["content"] as? String ?: "Patient Queues")
.setStyle(Notification.DecoratedCustomViewStyle()) .setStyle(Notification.DecoratedCustomViewStyle())
.setCustomContentView(collapesView) .setCustomContentView(collapesView)
.setCustomBigContentView(remoteViews) .setCustomBigContentView(remoteViews)
.setAutoCancel(false) .setPriority(Notification.PRIORITY_HIGH)
.setOngoing(true)
.setPriority(Notification.PRIORITY_LOW)
.setCategory(Notification.CATEGORY_EVENT) .setCategory(Notification.CATEGORY_EVENT)
.setVisibility(Notification.VISIBILITY_PUBLIC) .setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.build() .build()
} }
} }

@ -19,6 +19,7 @@ import 'package:hmg_patient_app_new/core/common_models/patient_queue.dart';
import 'package:hmg_patient_app_new/core/cache_consts.dart'; import 'package:hmg_patient_app_new/core/cache_consts.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart'; import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart'; import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/patient_queue_details_response_model.dart';
import 'package:hmg_patient_app_new/services/cache_service.dart'; import 'package:hmg_patient_app_new/services/cache_service.dart';
import 'package:live_activities/live_activities.dart' show LiveActivities; import 'package:live_activities/live_activities.dart' show LiveActivities;
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
@ -39,25 +40,26 @@ Future<dynamic> backgroundMessageHandler(dynamic message) async {
_incomingCall(message.data); _incomingCall(message.data);
return; return;
} }
if(message.data['isLiveActivity'] == true){ if (message.data['isLiveActivity'] == true || message.data['isLiveActivity'] == "true") {
print("live activity called"); print("live activity called");
LiveActivities().init(appGroupId: "group.alhabib.patientApp");
await LiveActivities().endAllActivities(); await LiveActivities().endAllActivities();
await Permission.notification.request(); // await Permission.notification.request();
PatientQueue _patientQueue = var buttonText = Utils.getCardButtonText(int.parse(message.data['callType']??"1"), message.data['roomNo']);
PatientQueue( PatientQueue _patientQueue = PatientQueue(
queueNo: message.data['queueNo']??"IME-1", queueNo: message.data['queueNo'] ?? "IME-1",
currentlyServingStatus: (message.data['callType']??0) == 1 ? "Call for vital signs".needTranslation : "Call for Doctor".needTranslation, currentlyServingStatus: (message.data['servingCallType'] ?? 0) == "1" ? "Call for vital signs".needTranslation : "Call for Doctor".needTranslation,
currentlyServing: message.data['currentlyServing']??"IME-3", currentlyServingTitle: message.data['servingQueueNo'] ?? "IME-3",
nextTicketStatus: message.data['nextTicketStatus']??"Please wait! you will be called for vital signs", nextTicketStatus: buttonText,
nextTicketBackground: message.data['nextTicketBackground']??2 callType: int.parse(message.data['callType']??"1"),
); currentlyServingCallType: int.parse(message.data['servingCallType']??"1"));
var activity= await LiveActivities().createActivity(
await LiveActivities().createActivity(
DateTime.now().millisecondsSinceEpoch.toString(), DateTime.now().millisecondsSinceEpoch.toString(),
_patientQueue.toMap(), _patientQueue.toMap(),
); );
print("the activity is $activity");
} }
} }
@ -284,7 +286,18 @@ class PushNotificationHandler {
init(BuildContext context) async { init(BuildContext context) async {
this.context = context; this.context = context;
final PermissionStatus status = await Permission.notification.request();
if (status.isGranted) {
// Notification permissions granted
print("Notification permissions granted");
} else if (status.isDenied) {
// Notification permissions denied
print("Notification permissions not granted");
} else if (status.isPermanentlyDenied) {
// // Notification permissions permanently denied, open app settings
// await openAppSettings();
}
if (Platform.isIOS) { if (Platform.isIOS) {
voIPKit.getVoIPToken().then((value) { voIPKit.getVoIPToken().then((value) {
print("🎈 APNS VOIP KIT TOKEN: $value"); print("🎈 APNS VOIP KIT TOKEN: $value");
@ -337,19 +350,29 @@ class PushNotificationHandler {
// timeOutTimer.cancel(); // timeOutTimer.cancel();
}; };
} }
try {
if (Platform.isAndroid) { final fcmToken = await FirebaseMessaging.instance.getToken().catchError((err) {
try { print(err);
final fcmToken = await FirebaseMessaging.instance.getToken().catchError((err) { });
print(err); if (fcmToken != null) onToken(fcmToken);
}); // }
if (fcmToken != null) onToken(fcmToken); } catch (ex) {
// } print("Notification Exception: $ex");
} catch (ex) {
print("Notification Exception: $ex");
}
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
} }
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
// if (Platform.isAndroid) {
// try {
// final fcmToken = await FirebaseMessaging.instance.getToken().catchError((err) {
// print(err);
// });
// if (fcmToken != null) onToken(fcmToken);
// // }
// } catch (ex) {
// print("Notification Exception: $ex");
// }
// FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
// }
if (Platform.isIOS) { if (Platform.isIOS) {
final permission = await FirebaseMessaging.instance.requestPermission(); final permission = await FirebaseMessaging.instance.requestPermission();
@ -367,6 +390,7 @@ class PushNotificationHandler {
try { try {
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) async { FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) async {
if (message != null) { if (message != null) {
if (Platform.isIOS) { if (Platform.isIOS) {
await Future.delayed(Duration(milliseconds: 3000)).then((value) { await Future.delayed(Duration(milliseconds: 3000)).then((value) {
@ -436,24 +460,22 @@ class PushNotificationHandler {
if(remoteMessage.data['isLiveActivity'] == true || remoteMessage.data['isLiveActivity'] == 'true'){ if(remoteMessage.data['isLiveActivity'] == true || remoteMessage.data['isLiveActivity'] == 'true'){
print("live activity called"); print("live activity called");
LiveActivities().init(appGroupId: "group.alhabib.patientApp");
await LiveActivities().endAllActivities(); await LiveActivities().endAllActivities();
await Permission.notification.request(); // await Permission.notification.request();
var buttonText = Utils.getCardButtonText(int.parse(remoteMessage.data['callType']??"1"), remoteMessage.data['roomNo']);
PatientQueue _patientQueue = PatientQueue _patientQueue =
PatientQueue( PatientQueue(
title: remoteMessage.data['title']??"HMG", queueNo: remoteMessage.data['queueNo'] ?? "IME-1",
contentText: remoteMessage.data['contentText']??"HMG Queue line", currentlyServingStatus: (remoteMessage.data['servingCallType'] ?? 0) == 1 ? "Call for vital signs".needTranslation : "Call for Doctor".needTranslation,
nextTicket: remoteMessage.data['nextTicket']??"IME-1", currentlyServingTitle: remoteMessage.data['servingQueueNo'] ?? "IME-3",
currentlyServingStatus: remoteMessage.data['currentlyServingStatus']??"Call for vital signs", nextTicketStatus: buttonText,
currentlyServing: remoteMessage.data['currentlyServing']??"IME-3", callType: int.parse(remoteMessage.data['callType']??"1"),currentlyServingCallType: int.parse(remoteMessage.data['servingCallType']??"1"));
nextTicketStatus: remoteMessage.data['nextTicketStatus']??"Please wait! you will be called for vital signs", var activity = await LiveActivities().createActivity(
nextTicketBackground: remoteMessage.data['nextTicketBackground']??2
);
await LiveActivities().createActivity(
DateTime.now().millisecondsSinceEpoch.toString(), DateTime.now().millisecondsSinceEpoch.toString(),
_patientQueue.toMap(), _patientQueue.toMap(),
); );
print("the activity is $activity");
} }
// GetNotificationsResponseModel notification = new GetNotificationsResponseModel(); // GetNotificationsResponseModel notification = new GetNotificationsResponseModel();
// //

Loading…
Cancel
Save