Skip to content
Merged
Changes from all 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
77 changes: 14 additions & 63 deletions src/services/push-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@
await this.setupAndroidNotificationChannels();

// Configure notifications behavior
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
shouldShowBanner: true,
shouldShowList: true,
}),
});
//Notifications.setNotificationHandler({
// handleNotification: async () => ({
// shouldShowAlert: true,
// shouldPlaySound: true,
// shouldSetBadge: false,
// shouldShowBanner: true,
// shouldShowList: true,
// }),
//});

// Set up notification listeners and store the subscription handles
this.notificationReceivedListener = Notifications.addNotificationReceivedListener(this.handleNotificationReceived);
this.notificationResponseListener = Notifications.addNotificationResponseReceivedListener(this.handleNotificationResponse);
//this.notificationReceivedListener = Notifications.addNotificationReceivedListener(this.handleNotificationReceived);
//this.notificationResponseListener = Notifications.addNotificationResponseReceivedListener(this.handleNotificationResponse);

// Listen for foreground messages and store the unsubscribe function
this.fcmOnMessageUnsubscribe = messaging().onMessage(async (remoteMessage) => {
Expand Down Expand Up @@ -198,11 +198,12 @@
}

// Get the token using the non-Expo push notification service method
const devicePushToken = await Notifications.getDevicePushTokenAsync();
//const devicePushToken = await Notifications.getDevicePushTokenAsync();

// The token format depends on the platform
const token = Platform.OS === 'ios' ? devicePushToken.data : devicePushToken.data;
//const token = Platform.OS === 'ios' ? devicePushToken.data : devicePushToken.data;

const token = await messaging().getToken();
this.pushToken = token as string;

logger.info({
Expand Down Expand Up @@ -232,60 +233,10 @@
}
}

// Method to send the token to your backend
private async sendTokenToBackend(token: string, unitId: string): Promise<void> {
// Implement your API call to register the token with your backend
// This is where you would associate the token with the unitId
try {
// Example implementation:
// await api.post('/register-push-token', { token, unitId });

logger.info({
message: 'Push token registered with backend',
context: { token, unitId },
});
} catch (error) {
logger.error({
message: 'Failed to register push token with backend',
context: { error, token, unitId },
});
}
}

public getPushToken(): string | null {
return this.pushToken;
}

public async sendTestNotification(): Promise<void> {
if (!this.pushToken) {
logger.warn({
message: 'Cannot send test notification - no push token available',
});
return;
}

try {
// This is a local test notification, not sent through a server
await Notifications.scheduleNotificationAsync({
content: {
title: 'Test Notification',
body: 'This is a test notification from Resgrid Unit',
data: { type: 'test', timestamp: new Date().toISOString() },
},
trigger: null, // Send immediately
});

logger.info({
message: 'Test notification sent',
});
} catch (error) {
logger.error({
message: 'Failed to send test notification',
context: { error },
});
}
}

public cleanup(): void {
if (this.notificationReceivedListener) {
this.notificationReceivedListener.remove();
Expand Down Expand Up @@ -343,6 +294,6 @@

return {
pushToken: pushNotificationService.getPushToken(),
sendTestNotification: () => pushNotificationService.sendTestNotification(),

Check failure on line 297 in src/services/push-notification.ts

View workflow job for this annotation

GitHub Actions / test

Property 'sendTestNotification' does not exist on type 'PushNotificationService'.
};
};
Loading