Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void start(ReadableMap config, Promise promise) {
: "[PLACEHOLDER] Tap to return to the call.";
String notificationContent = config.hasKey("notificationContent") ? config.getString("notificationContent")
: "[PLACEHOLDER] Your video call is ongoing";
String importance = config.hasKey("importance") ? config.getString("importance") : "high";
Comment thread
MiloszFilimowski marked this conversation as resolved.

int[] foregroundServiceTypes = buildForegroundServiceTypes(enableCamera, enableMicrophone);
if (foregroundServiceTypes.length == 0) {
Expand All @@ -49,6 +50,7 @@ public void start(ReadableMap config, Promise promise) {
serviceIntent.putExtra("channelName", channelName);
serviceIntent.putExtra("notificationTitle", notificationTitle);
serviceIntent.putExtra("notificationContent", notificationContent);
serviceIntent.putExtra("importance", importance);
serviceIntent.putExtra("foregroundServiceTypes", foregroundServiceTypes);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public void restartService(Intent intent) {
String channelName = intent.getStringExtra("channelName");
String notificationTitle = intent.getStringExtra("notificationTitle");
String notificationContent = intent.getStringExtra("notificationContent");
String importance = intent.getStringExtra("importance");
if (importance == null) {
importance = "high";
}
int[] foregroundServiceTypesArray = intent.getIntArrayExtra("foregroundServiceTypes");

if (channelId == null || channelName == null || notificationTitle == null || notificationContent == null) {
Expand All @@ -56,20 +60,35 @@ public void restartService(Intent intent) {
}
}

PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(getPackageName());
if (launchIntent != null) {
launchIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Comment thread
MiloszFilimowski marked this conversation as resolved.
Outdated
}
PendingIntent pendingIntent = launchIntent == null
? null
: PendingIntent.getActivity(
this, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

Notification notification = new NotificationCompat.Builder(this, channelId)
.setContentTitle(notificationTitle)
.setContentText(notificationContent)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setPriority(builderPriorityFor(importance))
.build();

createNotificationChannel(channelId, channelName);
createNotificationChannel(channelId, channelName, importance);
startForegroundWithNotification(notification, foregroundServiceType);
}

private static int channelImportanceFor(String importance) {
return "low".equals(importance) ? NotificationManager.IMPORTANCE_LOW : NotificationManager.IMPORTANCE_HIGH;
}

private static int builderPriorityFor(String importance) {
return "low".equals(importance) ? NotificationCompat.PRIORITY_LOW : NotificationCompat.PRIORITY_HIGH;
}

private void startForegroundWithNotification(Notification notification, int foregroundServiceType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(FOREGROUND_SERVICE_ID, notification, foregroundServiceType);
Expand All @@ -78,13 +97,13 @@ private void startForegroundWithNotification(Notification notification, int fore
}
}

private void createNotificationChannel(String channelId, String channelName) {
private void createNotificationChannel(String channelId, String channelName, String importance) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}

NotificationChannel serviceChannel =
new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
new NotificationChannel(channelId, channelName, channelImportanceFor(importance));
NotificationManager notificationManager = getSystemService(NotificationManager.class);
Comment thread
MiloszFilimowski marked this conversation as resolved.
if (notificationManager != null) {
notificationManager.createNotificationChannel(serviceChannel);
Expand Down
4 changes: 4 additions & 0 deletions src/useForegroundService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ForegroundServiceConfig = {
channelName?: string;
notificationTitle?: string;
notificationContent?: string;
importance?: 'low' | 'high';
};

const requestNotificationsPermission = async () => {
Expand All @@ -36,6 +37,7 @@ const useForegroundServiceAndroid = ({
channelName,
notificationContent,
notificationTitle,
importance,
}: ForegroundServiceConfig) => {
const [isConfigured, setIsConfigured] = useState(false);

Expand All @@ -51,6 +53,7 @@ const useForegroundServiceAndroid = ({
channelName,
notificationContent,
notificationTitle,
importance,
}).catch(console.error);
}, [
channelId,
Expand All @@ -61,6 +64,7 @@ const useForegroundServiceAndroid = ({
isConfigured,
notificationContent,
notificationTitle,
importance,
]);

useEffect(() => {
Expand Down
Loading