live activity handler

live_activities
tahaalam 3 weeks ago
parent d232cead63
commit 841f740cbd

@ -1,15 +1,18 @@
package com.ejada.hmg.live_activity
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content. Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import android.widget.RemoteViews
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.IconCompat
@ -36,6 +39,9 @@ class CustomLiveActivityManager(context: Context) :
)
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) {
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)
@ -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)
}
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.status, "setBackgroundResource", currentServingBackground)
remoteViews.setInt(R.id.container, "setBackgroundResource", containerBackground)
remoteViews.setTextColor(R.id.nextStep, ContextCompat.getColor(context, textColor))
remoteViews.setTextViewText(R.id.countvalue, data["queueNo"] 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 ?: "")
}
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) {
0 -> R.drawable.whitebg_yellow_border_rounded_corner_widget
@ -70,11 +83,12 @@ class CustomLiveActivityManager(context: Context) :
event: String,
data: Map<String, Any>
): Notification {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (notificationManager != null) {
notificationManager.cancelAll();
}
// val notificationManager =
// context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// if (notificationManager != null) {
// notificationManager.cancelAll();
// }
collapesView.apply {
updateCollapsedView(data)
}
@ -86,15 +100,14 @@ class CustomLiveActivityManager(context: Context) :
.setSmallIcon(R.drawable.hmg)
.setContentTitle(data["title"] as? String ?: "Patient Queue")
.setContentIntent(pendingIntent)
.setContentText(data["content"] as? String ?: "Patient Queue")
.setContentText(data["content"] as? String ?: "Patient Queues")
.setStyle(Notification.DecoratedCustomViewStyle())
.setCustomContentView(collapesView)
.setCustomBigContentView(remoteViews)
.setAutoCancel(false)
.setOngoing(true)
.setPriority(Notification.PRIORITY_LOW)
.setPriority(Notification.PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_EVENT)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.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/utils/utils.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:live_activities/live_activities.dart' show LiveActivities;
import 'package:permission_handler/permission_handler.dart';
@ -39,25 +40,26 @@ Future<dynamic> backgroundMessageHandler(dynamic message) async {
_incomingCall(message.data);
return;
}
if(message.data['isLiveActivity'] == true){
if (message.data['isLiveActivity'] == true || message.data['isLiveActivity'] == "true") {
print("live activity called");
LiveActivities().init(appGroupId: "group.alhabib.patientApp");
await LiveActivities().endAllActivities();
await Permission.notification.request();
PatientQueue _patientQueue =
PatientQueue(
queueNo: message.data['queueNo']??"IME-1",
currentlyServingStatus: (message.data['callType']??0) == 1 ? "Call for vital signs".needTranslation : "Call for Doctor".needTranslation,
currentlyServing: message.data['currentlyServing']??"IME-3",
nextTicketStatus: message.data['nextTicketStatus']??"Please wait! you will be called for vital signs",
nextTicketBackground: message.data['nextTicketBackground']??2
);
await LiveActivities().createActivity(
// await Permission.notification.request();
var buttonText = Utils.getCardButtonText(int.parse(message.data['callType']??"1"), message.data['roomNo']);
PatientQueue _patientQueue = PatientQueue(
queueNo: message.data['queueNo'] ?? "IME-1",
currentlyServingStatus: (message.data['servingCallType'] ?? 0) == "1" ? "Call for vital signs".needTranslation : "Call for Doctor".needTranslation,
currentlyServingTitle: message.data['servingQueueNo'] ?? "IME-3",
nextTicketStatus: buttonText,
callType: int.parse(message.data['callType']??"1"),
currentlyServingCallType: int.parse(message.data['servingCallType']??"1"));
var activity= await LiveActivities().createActivity(
DateTime.now().millisecondsSinceEpoch.toString(),
_patientQueue.toMap(),
);
print("the activity is $activity");
}
}
@ -284,7 +286,18 @@ class PushNotificationHandler {
init(BuildContext context) async {
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) {
voIPKit.getVoIPToken().then((value) {
print("🎈 APNS VOIP KIT TOKEN: $value");
@ -337,19 +350,29 @@ class PushNotificationHandler {
// timeOutTimer.cancel();
};
}
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);
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.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) {
final permission = await FirebaseMessaging.instance.requestPermission();
@ -367,6 +390,7 @@ class PushNotificationHandler {
try {
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) async {
if (message != null) {
if (Platform.isIOS) {
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
@ -436,24 +460,22 @@ class PushNotificationHandler {
if(remoteMessage.data['isLiveActivity'] == true || remoteMessage.data['isLiveActivity'] == 'true'){
print("live activity called");
LiveActivities().init(appGroupId: "group.alhabib.patientApp");
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(
title: remoteMessage.data['title']??"HMG",
contentText: remoteMessage.data['contentText']??"HMG Queue line",
nextTicket: remoteMessage.data['nextTicket']??"IME-1",
currentlyServingStatus: remoteMessage.data['currentlyServingStatus']??"Call for vital signs",
currentlyServing: remoteMessage.data['currentlyServing']??"IME-3",
nextTicketStatus: remoteMessage.data['nextTicketStatus']??"Please wait! you will be called for vital signs",
nextTicketBackground: remoteMessage.data['nextTicketBackground']??2
);
await LiveActivities().createActivity(
queueNo: remoteMessage.data['queueNo'] ?? "IME-1",
currentlyServingStatus: (remoteMessage.data['servingCallType'] ?? 0) == 1 ? "Call for vital signs".needTranslation : "Call for Doctor".needTranslation,
currentlyServingTitle: remoteMessage.data['servingQueueNo'] ?? "IME-3",
nextTicketStatus: buttonText,
callType: int.parse(remoteMessage.data['callType']??"1"),currentlyServingCallType: int.parse(remoteMessage.data['servingCallType']??"1"));
var activity = await LiveActivities().createActivity(
DateTime.now().millisecondsSinceEpoch.toString(),
_patientQueue.toMap(),
);
print("the activity is $activity");
}
// GetNotificationsResponseModel notification = new GetNotificationsResponseModel();
//

Loading…
Cancel
Save