1import PushNotificationIOS from '@react-native-community/push-notification-ios';
2import { Platform } from 'react-native';
3import PushNotification from 'react-native-push-notification';
4
5class NotificationManager {
6 configure = () => {
7 PushNotification.configure({
8 onRegister: function (token: any) {
9 console.log('TOKEN:', token);
10 },
11 onNotification: function (notification: any) {
12 console.log('NOTIFICATION:', notification);
13 notification.finish(PushNotificationIOS.FetchResult.NoData);
14 },
15 permissions: {
16 alert: true,
17 badge: true,
18 sound: true,
19 },
20 popInitialNotification: true,
21 requestPermissions: true,
22 });
23 PushNotification.createChannel(
24 {
25 channelId: 'fcm_fallback_notification_channel', // (required)
26 channelName: 'Channel', // (required)
27 },
28 (created) => console.log(`createChannel returned '${created}`),
29 );
30 };
31
32 buildAdroidNotification = (id: number, title: string, message: string, data = {}, options = {}) => {
33 return {
34 id: id,
35 autoCancel: true,
36 largeIcon: options.largeIcon || 'ic_launcher',
37 smallIcon: options.smallIcon || 'ic_launcher',
38 bigText: message || '',
39 subText: title || '',
40 vibration: options.vibration || 300,
41 vibrate: options.vibrate || false,
42 priority: options.priority || 'high',
43 importance: options.importance || 'high',
44 data: data,
45 };
46 };
47 buildIOSNotification = (id: number, title: string, message: string, data = {}, options = {}) => {
48 return {
49 alertAction: options.alertAction || 'view',
50 category: options.category || '',
51 userInfo: {
52 id: id,
53 item: data,
54 },
55 };
56 };
57 cancelAllNotification = () => {
58 console.log('cancel');
59 PushNotification.cancelAllLocalNotifications();
60 if (Platform.OS === 'ios') {
61 PushNotificationIOS.removeAllDeliveredNotifications();
62 }
63 };
64
65 showNotification = (id: number, title: string, message: string, data = {}, options = {}, date: Date) => {
66 PushNotification.localNotificationSchedule({
67 //Android
68 ...this.buildAdroidNotification(id, title, message, data, options),
69
70 // iOS
71 ...this.buildIOSNotification(id, title, message, data, options),
72
73 // Android and iOS
74 title: title || '',
75 message: message || '',
76 playSound: options.playSound || false,
77 soundName: options.soundName || 'default',
78 date: date,
79 });
80 };
81 unregister = () => {
82 PushNotification.unregister();
83 };
84}
85export const notificationManager = new NotificationManager();
86
1/**
2 * @format
3 */
4
5import PushNotificationIOS from '@react-native-community/push-notification-ios';
6import { AppRegistry } from 'react-native';
7import PushNotification from 'react-native-push-notification';
8import App from './App';
9import { name as appName } from './app.json';
10
11// Must be outside of any component LifeCycle (such as `componentDidMount`).
12PushNotification.configure({
13 // (optional) Called when Token is generated (iOS and Android)
14 onRegister: function (token) {
15 console.log('TOKEN:', token);
16 },
17
18 // (required) Called when a remote is received or opened, or local notification is opened
19 onNotification: function (notification) {
20 console.log('NOTIFICATION:', notification);
21
22 // process the notification
23 // (required) Called when a remote is received or opened, or local notification is opened
24 notification.finish(PushNotificationIOS.FetchResult.NoData);
25 },
26
27 // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
28 onAction: function (notification) {
29 console.log('ACTION:', notification.action);
30 console.log('NOTIFICATION:', notification);
31 // process the action
32 },
33
34 // (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)
35 onRegistrationError: function (err) {
36 console.error(err.message, err);
37 },
38
39 // IOS ONLY (optional): default: all - Permissions to register.
40 permissions: {
41 alert: true,
42 badge: true,
43 sound: true,
44 },
45
46 // Should the initial notification be popped automatically
47 // default: true
48
49 popInitialNotification: true,
50
51 /**
52 * (optional) default: true
53 * - Specified if permissions (ios) and token (android and ios) will requested or not,
54 * - if not, you must call PushNotificationsHandler.requestPermissions() later
55 * - if you are not using remote notification or do not have Firebase installed, use this:
56 * requestPermissions: Platform.OS === 'ios'
57 */
58
59 requestPermissions: true,
60});
61
62AppRegistry.registerComponent(appName, () => App);
63