Guest User

Untitled

a guest
Jan 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. import PushNotification from 'react-native-push-notification';
  2. import OneSignal from 'react-native-onesignal';
  3. import { store, Settings } from './';
  4. import { Notifications } from '../actions';
  5.  
  6. class NotificationController {
  7.  
  8. constructor () {
  9. this.store = store;
  10. this.token = null;
  11. this.userId = null;
  12. this.device = {};
  13. this._listeners = [];
  14. this.configurePushNotification();
  15. this.configureOneSignal();
  16.  
  17. OneSignal.enableVibrate(true);
  18. OneSignal.enableSound(true);
  19. OneSignal.enableNotificationsWhenActive(true);
  20. OneSignal.setSubscription(true);
  21. }
  22.  
  23. configurePushNotification () {
  24. const self = this;
  25. PushNotification.configure({
  26. onRegister(token) {
  27. console.log('Registered pushNotificationToken:', token);
  28. self.token = token;
  29. self.store.dispatch(Notifications.saveDeviceToken(token));
  30. },
  31. onNotification(notification) {
  32. console.log("Received pushNotification", notification);
  33. let notificationData = notification.data;
  34. if (notificationData && notificationData.remote === true && notificationData.custom && notificationData.custom.a && notificationData.custom.a.type) {
  35. let message = {};
  36. switch (notificationData.custom.a.type) {
  37. case 'alert':
  38. message = {
  39. id: notificationData.notificationId,
  40. title: notificationData.custom.a.title,
  41. message: notificationData.custom.a.content,
  42. playSound: true,
  43. category: 'TEST'
  44. };
  45. break
  46. default:
  47. message = null;
  48. }
  49. console.log('Pushing Notification:', message);
  50. if (message) PushNotification.localNotification(message);
  51. PushNotification.localNotificationSchedule({
  52. message: "My Notification Message", // (required)
  53. date: new Date(Date.now() + (5 * 1000)) // in 60 secs
  54. });
  55. }
  56. self._listeners.forEach((listener) => {
  57. try {
  58. listener(notification);
  59. } catch (err) {
  60. console.warn("Listener failed to process push notification", err);
  61. }
  62. });
  63. },
  64. senderID: Settings.notifications.gcm,
  65. permissions: {
  66. alert: true,
  67. badge: true,
  68. sound: true
  69. },
  70. popInitialNotification: true,
  71. requestPermissions: true,
  72. });
  73. }
  74.  
  75. configureOneSignal () {
  76. const self = this;
  77. OneSignal.configure({
  78. onIdsAvailable (device) {
  79. console.log('UserId = ', device.userId);
  80. console.log('PushToken = ', device.pushToken);
  81. self.userId = device.userId;
  82. self.device = device;
  83. self.store.dispatch(Notifications.saveNotificationUserId(device.userId));
  84. },
  85. onNotification (notification) {
  86. console.log('Notification from OneSignal', notification);
  87. },
  88. onNotificationOpened (message, data, isActive) {
  89. console.log('MESSAGE: ', message);
  90. console.log('DATA: ', data);
  91. console.log('ISACTIVE: ', isActive);
  92. // Do whatever you want with the objects here
  93. // _navigator.to('main.post', data.title, { // If applicable
  94. // article: {
  95. // title: data.title,
  96. // link: data.url,
  97. // action: data.actionSelected
  98. // }
  99. // });
  100. }
  101. });
  102. }
  103.  
  104. addListener(listener) {
  105. this._listeners.push(listener);
  106. return () => {
  107. this._listeners = this._listeners.filter((l) => l !== listener);
  108. }
  109. }
  110.  
  111. token() {
  112. return this.token;
  113. }
  114.  
  115. userId() {
  116. return this.userId;
  117. }
  118.  
  119. device() {
  120. return this.device;
  121. }
  122.  
  123. popInitial() {
  124. PushNotification.configure({
  125. popInitialNotification: true,
  126. });
  127. }
  128. }
  129.  
  130. export default new NotificationController();
Add Comment
Please, Sign In to add comment