Skip to content

Commit

Permalink
dep(@notifee/react-native): registerForegroundService as early as pos…
Browse files Browse the repository at this point in the history
…sible
  • Loading branch information
Calinteodor committed Dec 10, 2024
1 parent 3b67930 commit a9d6e2b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
24 changes: 24 additions & 0 deletions index.android.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
import './react/index.native';
import notifee, {EventType} from '@notifee/react-native';

Check failure on line 2 in index.android.js

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups

Check failure on line 2 in index.android.js

View workflow job for this annotation

GitHub Actions / Lint

A space is required after '{'

Check failure on line 2 in index.android.js

View workflow job for this annotation

GitHub Actions / Lint

A space is required before '}'
import logger from './react/features/app/logger';

// Needs to be registered outside any React components as early as possible
await notifee.registerForegroundService(() => {

Check failure on line 6 in index.android.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return new Promise(resolve => {
logger.warn('Foreground service running');

notifee.onBackgroundEvent(async event => {
if (event.type === EventType.ACTION_PRESS && event.detail.pressAction.id === 'hang-up') {
console.log('HANG UP BUTTON PRESSED BACKGROUND');
}

return resolve();
});

notifee.onForegroundEvent(async ({ type, detail }) => {
if (type === EventType.ACTION_PRESS && detail.pressAction.id === 'hang-up') {
console.log('HANG UP BUTTON PRESSED');
}

return resolve();
});
});
});
33 changes: 12 additions & 21 deletions react/features/conference/components/functions.native.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IReduxState } from '../../app/types';

Check failure on line 1 in react/features/conference/components/functions.native.ts

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups

Check failure on line 1 in react/features/conference/components/functions.native.ts

View workflow job for this annotation

GitHub Actions / Lint

`../../app/types` import should occur after import of `../../app/logger`
import notifee, { AndroidImportance, AndroidVisibility, AuthorizationStatus } from '@notifee/react-native';
import notifee, {AndroidImportance, AndroidVisibility, AuthorizationStatus, EventType} from '@notifee/react-native';

Check failure on line 2 in react/features/conference/components/functions.native.ts

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups

Check failure on line 2 in react/features/conference/components/functions.native.ts

View workflow job for this annotation

GitHub Actions / Lint

A space is required after '{'

Check failure on line 2 in react/features/conference/components/functions.native.ts

View workflow job for this annotation

GitHub Actions / Lint

'EventType' is defined but never used

Check failure on line 2 in react/features/conference/components/functions.native.ts

View workflow job for this annotation

GitHub Actions / Lint

A space is required before '}'
import logger from "../../app/logger";

export * from './functions.any';
Expand Down Expand Up @@ -35,14 +35,7 @@ export const isConnecting = (state: IReduxState) => {

export async function displayNotificationAsForegroundService() {

// Foreground service needs to be registered first
notifee.registerForegroundService(() => {
return new Promise(() => {
logger.warn('Foreground service running');
});
});

// Request permissions
// Request permissions if not authorized
const currentPermissions = await notifee.getNotificationSettings();

if (currentPermissions.authorizationStatus !== AuthorizationStatus.AUTHORIZED) {
Expand All @@ -56,27 +49,24 @@ export async function displayNotificationAsForegroundService() {
visibility: AndroidVisibility.PUBLIC
});

// Display notification
// Display notification as a foreground service
await notifee.displayNotification({
title: '<p style="color: #4caf50;"><b>Ongoing conference</span></p></b></p> &#128576;',
subtitle: '&#129395;',
title: 'Ongoing conference',
body:
'Meeting <p style="text-decoration: line-through">you are </p> participating <p style="color: #ffffff; background-color: #9c27b0"><i>is ongoing</i></p> &#127881;!',
'Meeting you are participating is ongoing.',
android: {
asForegroundService: true,
autoCancel: false,
channelId,
importance: AndroidImportance.HIGH,
color: '#4caf50',
ongoing: true,
smallIcon: 'ic_notification',
actions: [
{
title: '<b>Dance</b> &#128111;',
pressAction: { id: 'dance' },
},
{
title: '<p style="color: #f44336;"><b>Cry</b> &#128557;</p>',
pressAction: { id: 'cry' },
title: 'Hang Up',
pressAction: {
id: 'hang-up'
},
},
],
},
Expand All @@ -86,7 +76,8 @@ export async function displayNotificationAsForegroundService() {
export async function stopForegroundService() {
try {
await notifee.stopForegroundService();
logger.warn('Foreground service stopped.')
} catch (err) {
console.log(err);
logger.error(err);
}
}
13 changes: 9 additions & 4 deletions react/features/conference/components/native/Conference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ class Conference extends AbstractConference<IProps, State> {

BackHandler.addEventListener('hardwareBackPress', this._onHardwareBackPress);

displayNotificationAsForegroundService();

if (_audioOnlyEnabled && _startCarMode) {
navigation.navigate(screen.conference.carmode);
}
Expand All @@ -232,6 +230,10 @@ class Conference extends AbstractConference<IProps, State> {
_startCarMode
} = this.props;

if (Platform.OS === 'android') {
displayNotificationAsForegroundService();
}

if (!prevProps._showLobby && _showLobby) {
navigate(screen.lobby.root);
}
Expand All @@ -254,11 +256,14 @@ class Conference extends AbstractConference<IProps, State> {
* @returns {void}
*/
componentWillUnmount() {

if (Platform.OS === 'android') {
stopForegroundService();
}

// Tear handling any hardware button presses for back navigation down.
BackHandler.removeEventListener('hardwareBackPress', this._onHardwareBackPress);

stopForegroundService();

clearTimeout(this._expandedLabelTimeout.current ?? 0);
}

Expand Down

0 comments on commit a9d6e2b

Please sign in to comment.