153 lines
4.5 KiB
TypeScript
153 lines
4.5 KiB
TypeScript
// @ts-ignore
|
|
import PushNotification from 'react-native-push-notification';
|
|
import { AppState, AppStateStatus } from 'react-native';
|
|
|
|
/**
|
|
* Creates a notification channel for Android
|
|
*/
|
|
export const createNotificationChannel = (): void => {
|
|
PushNotification.createChannel(
|
|
{
|
|
channelId: 'default-channel-id',
|
|
channelName: 'Default Channel',
|
|
channelDescription: 'A default channel for notifications',
|
|
playSound: true,
|
|
soundName: 'default',
|
|
importance: 4, // High importance
|
|
vibrate: true,
|
|
},
|
|
(created: boolean) => console.log(`Notification channel created: ${created}`)
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Configures notification settings
|
|
*/
|
|
export const configureNotificationSettings = (): void => {
|
|
PushNotification.configure({
|
|
// (optional) Called when Token is generated (iOS and Android)
|
|
onRegister: function (token: any) {
|
|
console.log('TOKEN:', token);
|
|
},
|
|
|
|
// (required) Called when a remote is received or opened, or local notification is opened
|
|
onNotification: function (notification: any) {
|
|
console.log('NOTIFICATION:', notification);
|
|
},
|
|
|
|
// (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
|
|
onAction: function (notification: any) {
|
|
console.log('ACTION:', notification.action);
|
|
console.log('NOTIFICATION:', notification);
|
|
},
|
|
|
|
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
|
|
onRegistrationError: function (err: any) {
|
|
console.error(err.message, err);
|
|
},
|
|
|
|
// IOS ONLY (optional): default: all - Permissions to register.
|
|
permissions: {
|
|
alert: true,
|
|
badge: true,
|
|
sound: true,
|
|
},
|
|
|
|
// Should the initial notification be popped automatically
|
|
// default: true
|
|
popInitialNotification: true,
|
|
|
|
/**
|
|
* (optional) default: true
|
|
* - Specified if permissions (ios) and token (android and ios) will requested or not,
|
|
* - if not, you must call PushNotificationsHandler.requestPermissions() later
|
|
* - if you are not using remote notification or do not have Firebase installed, use this:
|
|
* requestPermissions: Platform.OS === 'ios'
|
|
*/
|
|
requestPermissions: true,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Shows a local notification
|
|
*/
|
|
export const showLocalNotification = (notificationData: {
|
|
title: string;
|
|
message: string;
|
|
channelId?: string;
|
|
playSound?: boolean;
|
|
soundName?: string;
|
|
vibrate?: boolean;
|
|
priority?: 'min' | 'low' | 'default' | 'high' | 'max';
|
|
visibility?: 'private' | 'public' | 'secret';
|
|
}): void => {
|
|
PushNotification.localNotification({
|
|
channelId: notificationData.channelId || 'default-channel-id',
|
|
title: notificationData.title,
|
|
message: notificationData.message,
|
|
playSound: notificationData.playSound !== false,
|
|
soundName: notificationData.soundName || 'default',
|
|
vibrate: notificationData.vibrate !== false,
|
|
priority: notificationData.priority || 'high',
|
|
visibility: notificationData.visibility || 'private',
|
|
importance: 'high',
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Determines if a custom notification should be shown based on app state
|
|
*/
|
|
export const shouldShowCustomNotification = (appState: AppStateStatus): boolean => {
|
|
// Show notification when app is in background or inactive
|
|
return appState !== 'active';
|
|
};
|
|
|
|
/**
|
|
* Test function to trigger a sample notification
|
|
*/
|
|
export const testNotification = (): void => {
|
|
showLocalNotification({
|
|
title: 'Test Notification',
|
|
message: 'This is a test notification from T4B Chat app',
|
|
channelId: 'default-channel-id',
|
|
playSound: true,
|
|
vibrate: true,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Cancels all notifications
|
|
*/
|
|
export const cancelAllNotifications = (): void => {
|
|
PushNotification.cancelAllLocalNotifications();
|
|
};
|
|
|
|
/**
|
|
* Cancels a specific notification by ID
|
|
*/
|
|
export const cancelNotification = (notificationId: string): void => {
|
|
PushNotification.cancelLocalNotifications({ id: notificationId });
|
|
};
|
|
|
|
/**
|
|
* Gets the notification count
|
|
*/
|
|
export const getNotificationCount = (callback: (count: number) => void): void => {
|
|
PushNotification.getApplicationIconBadgeNumber(callback);
|
|
};
|
|
|
|
/**
|
|
* Sets the notification count
|
|
*/
|
|
export const setNotificationCount = (count: number): void => {
|
|
PushNotification.setApplicationIconBadgeNumber(count);
|
|
};
|
|
|
|
/**
|
|
* Clears all notifications and badge count
|
|
*/
|
|
export const clearAllNotifications = (): void => {
|
|
cancelAllNotifications();
|
|
setNotificationCount(0);
|
|
};
|