Advertisement
armadiazrino

Untitled

Sep 30th, 2022
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable import/default */
  2. import PushNotification, { Importance } from 'react-native-push-notification';
  3. import { isAndroid } from 'utils';
  4. import NotificationHandler from './NotificationHandler';
  5.  
  6. export default class NotifService {
  7.   constructor(onRegister, onNotification) {
  8.     this.lastId = 0;
  9.     this.lastChannelCounter = 0;
  10.  
  11.     this.createDefaultChannels();
  12.  
  13.     NotificationHandler.attachRegister(onRegister);
  14.     NotificationHandler.attachNotification(onNotification);
  15.  
  16.     // Clear badge number at start
  17.     PushNotification.getApplicationIconBadgeNumber(function (number) {
  18.       if (number > 0) {
  19.         PushNotification.setApplicationIconBadgeNumber(0);
  20.       }
  21.     });
  22.  
  23.     PushNotification.getChannels(function (channels) {
  24.       console.log(channels);
  25.     });
  26.   }
  27.  
  28.   createDefaultChannels() {
  29.     if (isAndroid) {
  30.       PushNotification.createChannel(
  31.         {
  32.           channelId: 'byu', // (required)
  33.           channelName: 'Byu', // (required)
  34.           channelDescription: 'Byu', // (optional) default: undefined.
  35.           importance: Importance.HIGH // (optional) default: Importance.HIGH. Int value of the Android notification importance
  36.         },
  37.         created =>
  38.           console.log(
  39.             `createChannel 'default-channel-id' returned '${created}'`
  40.           ) // (optional) callback returns whether the channel was created, false means it already existed.
  41.       );
  42.     }
  43.     // PushNotification.createChannel(
  44.     //   {
  45.     //     channelId: 'sound-channel-id', // (required)
  46.     //     channelName: 'Sound channel', // (required)
  47.     //     channelDescription: 'A sound channel', // (optional) default: undefined.
  48.     //     soundName: 'sample.mp3', // (optional) See `soundName` parameter of `localNotification` function
  49.     //     importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
  50.     //     vibrate: true // (optional) default: true. Creates the default vibration patten if true.
  51.     //   },
  52.     //   created =>
  53.     //     console.log(`createChannel 'sound-channel-id' returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
  54.     // );
  55.   }
  56.  
  57.   createOrUpdateChannel() {
  58.     this.lastChannelCounter++;
  59.     PushNotification.createChannel(
  60.       {
  61.         channelId: 'custom-channel-id', // (required)
  62.         channelName: `Custom channel - Counter: ${this.lastChannelCounter}`, // (required)
  63.         channelDescription: `A custom channel to categorise your custom notifications. Updated at: ${Date.now()}`, // (optional) default: undefined.
  64.         soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
  65.         importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
  66.         vibrate: true // (optional) default: true. Creates the default vibration patten if true.
  67.       },
  68.       created => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
  69.     );
  70.   }
  71.  
  72.   popInitialNotification() {
  73.     PushNotification.popInitialNotification(notification =>
  74.       console.log('InitialNotication:', notification)
  75.     );
  76.   }
  77.  
  78.   localNotif(soundName) {
  79.     this.lastId++;
  80.     PushNotification.localNotification({
  81.       /* Android Only Properties */
  82.       channelId: soundName ? 'sound-channel-id' : 'default-channel-id',
  83.       ticker: 'My Notification Ticker', // (optional)
  84.       autoCancel: true, // (optional) default: true
  85.       largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
  86.       smallIcon: 'ic_notification', // (optional) default: "ic_notification" with fallback for "ic_launcher"
  87.       bigText: 'My big text that will be shown when notification is expanded', // (optional) default: "message" prop
  88.       subText: 'This is a subText', // (optional) default: none
  89.       color: 'red', // (optional) default: system default
  90.       vibrate: true, // (optional) default: true
  91.       vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
  92.       tag: 'some_tag', // (optional) add tag to message
  93.       group: 'group', // (optional) add group to message
  94.       groupSummary: false, // (optional) set this notification to be the group summary for a group of notifications, default: false
  95.       ongoing: false, // (optional) set whether this is an "ongoing" notification
  96.       actions: ['Yes', 'No'], // (Android only) See the doc for notification actions to know more
  97.       invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true
  98.  
  99.       when: null, // (optionnal) Add a timestamp pertaining to the notification (usually the time the event occurred). For apps targeting Build.VERSION_CODES.N and above, this time is not shown anymore by default and must be opted into by using `showWhen`, default: null.
  100.       usesChronometer: false, // (optional) Show the `when` field as a stopwatch. Instead of presenting `when` as a timestamp, the notification will show an automatically updating display of the minutes and seconds since when. Useful when showing an elapsed time (like an ongoing phone call), default: false.
  101.       timeoutAfter: null, // (optional) Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled, default: null
  102.  
  103.       /* iOS only properties */
  104.       category: '', // (optional) default: empty string
  105.  
  106.       /* iOS and Android properties */
  107.       id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
  108.       title: 'Local Notification', // (optional)
  109.       message: 'My Notification Message', // (required)
  110.       userInfo: { screen: 'home' }, // (optional) default: {} (using null throws a JSON value '<null>' error)
  111.       playSound: !!soundName, // (optional) default: true
  112.       soundName: soundName ? soundName : 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
  113.       number: 10 // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
  114.     });
  115.   }
  116.  
  117.   scheduleNotif(soundName) {
  118.     this.lastId++;
  119.     PushNotification.localNotificationSchedule({
  120.       date: new Date(Date.now() + 30 * 1000), // in 30 secs
  121.  
  122.       /* Android Only Properties */
  123.       channelId: soundName ? 'sound-channel-id' : 'default-channel-id',
  124.       ticker: 'My Notification Ticker', // (optional)
  125.       autoCancel: true, // (optional) default: true
  126.       largeIcon: 'ic_launcher', // (optional) default: "ic_launcher"
  127.       smallIcon: 'ic_notification', // (optional) default: "ic_notification" with fallback for "ic_launcher"
  128.       bigText: 'My big text that will be shown when notification is expanded', // (optional) default: "message" prop
  129.       subText: 'This is a subText', // (optional) default: none
  130.       color: 'blue', // (optional) default: system default
  131.       vibrate: true, // (optional) default: true
  132.       vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
  133.       tag: 'some_tag', // (optional) add tag to message
  134.       group: 'group', // (optional) add group to message
  135.       groupSummary: false, // (optional) set this notification to be the group summary for a group of notifications, default: false
  136.       ongoing: false, // (optional) set whether this is an "ongoing" notification
  137.       actions: ['Yes', 'No'], // (Android only) See the doc for notification actions to know more
  138.       invokeApp: false, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true
  139.  
  140.       when: null, // (optionnal) Add a timestamp pertaining to the notification (usually the time the event occurred). For apps targeting Build.VERSION_CODES.N and above, this time is not shown anymore by default and must be opted into by using `showWhen`, default: null.
  141.       usesChronometer: false, // (optional) Show the `when` field as a stopwatch. Instead of presenting `when` as a timestamp, the notification will show an automatically updating display of the minutes and seconds since when. Useful when showing an elapsed time (like an ongoing phone call), default: false.
  142.       timeoutAfter: null, // (optional) Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled, default: null
  143.  
  144.       /* iOS only properties */
  145.       category: '', // (optional) default: empty string
  146.  
  147.       /* iOS and Android properties */
  148.       id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
  149.       title: 'Scheduled Notification', // (optional)
  150.       message: 'My Notification Message', // (required)
  151.       userInfo: { sceen: 'home' }, // (optional) default: {} (using null throws a JSON value '<null>' error)
  152.       playSound: !!soundName, // (optional) default: true
  153.       soundName: soundName ? soundName : 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
  154.       number: 10 // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
  155.     });
  156.   }
  157.  
  158.   checkPermission(cbk) {
  159.     return PushNotification.checkPermissions(cbk);
  160.   }
  161.  
  162.   requestPermissions() {
  163.     return PushNotification.requestPermissions();
  164.   }
  165.  
  166.   cancelNotif() {
  167.     PushNotification.cancelLocalNotifications({ id: '' + this.lastId });
  168.   }
  169.  
  170.   cancelAll() {
  171.     PushNotification.cancelAllLocalNotifications();
  172.   }
  173.  
  174.   abandonPermissions() {
  175.     PushNotification.abandonPermissions();
  176.   }
  177.  
  178.   getScheduledLocalNotifications(callback) {
  179.     PushNotification.getScheduledLocalNotifications(callback);
  180.   }
  181.  
  182.   getDeliveredNotifications(callback) {
  183.     PushNotification.getDeliveredNotifications(callback);
  184.   }
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement