Reference Source

src/util/notification.js

  1. import React, {Component} from 'react';
  2. import { Platform } from 'react-native';
  3. // import firebase from 'react-native-firebase';
  4.  
  5. /**
  6. * Class for handling notifications.
  7. */
  8. class Notification extends Component {
  9. /**
  10. * Creates an instance of Notification.
  11. * @constructor
  12. */
  13. constructor(props){
  14. super(props);
  15. //firebase.initializeApp(config);
  16. this.notification = null;
  17. }
  18.  
  19.  
  20. /**
  21. * Check if user has permission to receive notification
  22. * if has permission, then ask for a device token
  23. * if not, rejected the request and user will not receive notification
  24. */
  25. async checkPermission() {
  26. if (Platform.OS === 'ios') {
  27. return false;
  28. }
  29. const enabled = await firebase.messaging().hasPermission();
  30. //enabled: ask firebase if user has permission to request firebase messaging
  31. if (enabled){
  32. return this.getToken();
  33. }
  34. else{
  35. return false;
  36. }
  37.  
  38. }
  39.  
  40. /**
  41. * Request a device token
  42. */
  43. async getToken(){
  44. if (Platform.OS === 'ios') {
  45. return false;
  46. }
  47.  
  48. const fcmToken = await firebase.messaging().getToken();
  49. if (fcmToken) {
  50. //console.log(`token is:${fcmToken}`)
  51. const fcm = fcmToken;
  52. const newData = {data:{}};
  53. newData.data.id = AuthState.getCurrentUserID();
  54. newData.data.deviceToken = fcm ;
  55. console.log(fcm);
  56. const prepareUpdate = newData.data;
  57. Database.editProfileData(prepareUpdate, ()=>{this._callback(true)}, ()=>{this._callback(false)});
  58. // Database.editProfileData(prepareUpdate, (data) => {
  59. // this._callback(true);
  60. // },(error) => {
  61. // this._callback(false);
  62. // });
  63. } else {
  64. // user doesn't have a device token yet
  65. console.log('user doesnt have a device token yet');
  66. return false;
  67. }
  68. }
  69.  
  70. /**
  71. * Create a new channel for Android user to receive notification
  72. */
  73. createChannel(){
  74. if (Platform.OS === 'ios') {
  75. return null;
  76. }
  77.  
  78. const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
  79. .setDescription('My apps test channel');
  80. return (channel);
  81. }
  82. /**
  83. * Delete device token
  84. */
  85. async removeToken(){
  86. if (Platform.OS === 'ios') {
  87. return null;
  88. }
  89. await firebase.messaging().deleteToken();
  90. const prepareUpdate = {};
  91. prepareUpdate.id = AuthState.getCurrentUserID();
  92. prepareUpdate.deviceToken = "";
  93. Database.editProfileData(prepareUpdate, ()=>{this._callback(true)}, ()=>{this._callback(false)});
  94. }
  95.  
  96. deleteNotificationArea(){
  97. const prepareUpdate = {};
  98. prepareUpdate.id = AuthState.getCurrentUserID();
  99. prepareUpdate.hasCircle = false;
  100. Database.editProfileData(prepareUpdate, ()=>{this._callback(true)}, ()=>{this._callback(false)});
  101. }
  102.  
  103. }
  104.  
  105. const NotificationMethod = new Notification();
  106. export default NotificationMethod;