Pārlūkot izejas kodu

enhance firebase notification FCM

vbea 3 gadi atpakaļ
vecāks
revīzija
8d0bc1b7e2

+ 1 - 1
Strides-APP/android/app/build.gradle

@@ -3,7 +3,7 @@ apply plugin: 'com.google.gms.google-services'
 
 import com.android.build.OutputFile
 
-def myVersionName = "2.2.3" //★★★★★版本号★★★★★
+def myVersionName = "2.3.0" //★★★★★版本号★★★★★
 /**
  * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
  * and bundleReleaseJsAndAssets).

+ 8 - 8
Strides-APP/android/app/google-services.json

@@ -1,37 +1,37 @@
 {
   "project_info": {
-    "project_number": "779803805444",
-    "project_id": "strides-67073",
-    "storage_bucket": "strides-67073.appspot.com"
+    "project_number": "261971604133",
+    "project_id": "chargeco-1d845",
+    "storage_bucket": "chargeco-1d845.appspot.com"
   },
   "client": [
     {
       "client_info": {
-        "mobilesdk_app_id": "1:779803805444:android:273dfce7b0f8c7527af251",
+        "mobilesdk_app_id": "1:261971604133:android:43f6ca6f991d53aac8134c",
         "android_client_info": {
           "package_name": "com.strides.chargeco"
         }
       },
       "oauth_client": [
         {
-          "client_id": "779803805444-0nat1fh00njdj2d6c9fc13dc9k2gr5t9.apps.googleusercontent.com",
+          "client_id": "261971604133-nfdvptsc4qd98g078o69u5hjheg6c0hp.apps.googleusercontent.com",
           "client_type": 3
         }
       ],
       "api_key": [
         {
-          "current_key": "AIzaSyDRsTzDbIZTd-SWlSOZ6w8mKTuiS0eGZmE"
+          "current_key": "AIzaSyCN1UuC3Y5OnFmwJpAlVX-AUYkWbQCywvM"
         }
       ],
       "services": {
         "appinvite_service": {
           "other_platform_oauth_client": [
             {
-              "client_id": "779803805444-0nat1fh00njdj2d6c9fc13dc9k2gr5t9.apps.googleusercontent.com",
+              "client_id": "261971604133-nfdvptsc4qd98g078o69u5hjheg6c0hp.apps.googleusercontent.com",
               "client_type": 3
             },
             {
-              "client_id": "779803805444-7vilfstvrrjvolvc3mgq5iklpied7f5e.apps.googleusercontent.com",
+              "client_id": "261971604133-8fsde5cle4nhs3mjvs6nm99cdq87ij52.apps.googleusercontent.com",
               "client_type": 2,
               "ios_info": {
                 "bundle_id": "com.strides.chargeco"

+ 2 - 2
Strides-APP/android/app/version.properties

@@ -1,2 +1,2 @@
-#Thu Apr 27 14:34:47 CST 2023
-VERSION_CODE=207
+#Fri May 05 14:58:21 CST 2023
+VERSION_CODE=217

+ 3 - 3
Strides-APP/app.json

@@ -1,8 +1,8 @@
 {
   "name": "JuicePlus",
   "displayName": "ChargEco",
-  "versionCode": 210,
-  "versionName": "V2.2.3",
-  "product": true,
+  "versionCode": 220,
+  "versionName": "V2.3.0",
+  "product": false,
   "debug": true
 }

+ 1 - 1
Strides-APP/app/api/apiUpload.js

@@ -25,7 +25,7 @@ export default {
     return upload(prefix, data, {photoSubDir: type});
   },
   testNotification() {
-    return get('steve/devicesApi/googleNotification/doNotification');
+    return get('devicesApi/googleNotification/doNotification');
   }
 }
 

+ 8 - 2
Strides-APP/app/pages/home/Drawer.js

@@ -13,10 +13,11 @@ import Dialog from '../../components/Dialog';
 import { host, setAccessToken } from '../../api/http';
 import { getStorageJsonSync, setStorage, setStorageJson } from '../../utils/storage';
 import Button from '../../components/Button';
-import { AutoLogin, RegisterToken } from '../sign/Login';
+import { AutoLogin } from '../sign/Login';
 import apiCharge from '../../api/apiCharge';
 import { toTopupPage } from '../wallet/Payment';
 import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
+import utils from '../../utils/utils';
 
 const Drawer = createDrawerNavigator();
 
@@ -64,7 +65,12 @@ export default class Home extends Component {
           this.setState({
             userInfo: info
           });
-          RegisterToken();
+          if (info.firebaseToken) {
+            let token = isIOS ? info.firebaseToken?.ios : info.firebaseToken?.android
+            if (notifyToken.token) {
+              utils.registerFirebaseToken(token ?? "");
+            }
+          }
         }, true);
       });
     }

+ 56 - 13
Strides-APP/app/pages/home/Notify.js

@@ -5,13 +5,15 @@ import PushNotification from "react-native-push-notification";
 import Dialog from '../../components/Dialog';
 import apiUpload from '../../api/apiUpload';
 import app from '../../../app.json'
+import utils from '../../utils/utils';
 
 export default class Notify extends Component {
   constructor(props) {
     super(props);
     this.state = {
       permission: false,
-      notifyToken: 'none'
+      notifyToken: '',
+      serverToken: ''
     };
   }
 
@@ -22,6 +24,18 @@ export default class Notify extends Component {
         notifyToken: global.notifyToken.token
       })
     }
+    this.getUserToken();
+  }
+
+  getUserToken() {
+    getUserInfo(info => {
+      if (info.firebaseToken) {
+        let token = isIOS ? info.firebaseToken?.ios : info.firebaseToken?.android
+        this.setState({
+          serverToken: token ?? ""
+        });
+      }
+    }, true);
   }
 
   async requestUserPermission() {
@@ -79,36 +93,65 @@ export default class Notify extends Component {
     })
   }
 
+  updateToken() {
+    if (getUserId() > 0) {
+      Dialog.showProgressDialog();
+      utils.registerFirebaseToken("", res => {
+        Dialog.dismissLoading();
+        toastShort(res ? "Upload success" : "Upload failed")
+        if (res) {
+          this.getUserToken();
+        }
+      })
+    } else {
+      toastShort("Please login first");
+    }
+  }
+
   copyToken() {
-    Clipboard.setString(this.state.notifyToken);
-    toastShort('已复制到剪贴板')
+    if (this.state.notifyToken) {
+      Clipboard.setString(this.state.notifyToken);
+      toastShort('Copied')
+    }
   }
 
   render() {
     return (
       <View style={styles.container}>
-        <Text style={styles.text}>注册的Token:{this.state.notifyToken}</Text>
+        <Text style={styles.text}>Device Token:{this.state.notifyToken || "none"}</Text>
+        <Text style={styles.text}>Server Token:{this.state.serverToken || "none"}</Text>
+        { utils.isNotEmpty(this.state.notifyToken) &&
+          <View style={styles.text}>
+            <Button
+              text='Copy Token'
+              elevation={2}
+              onClick={() => this.copyToken()}
+              />
+          </View>
+        }
         <View style={styles.text}>
           <Button
-            text='复制Token'
-            elevation={2}
-            onClick={() => this.copyToken()}
-            />
-        </View>
-        <View style={styles.text}>
-          <Button
-            text='发送本地通知'
+            text='Send Local Notification'
             elevation={2}
             onClick={() => this.localNotification()}
             />
         </View>
         <View style={styles.text}>
           <Button
-            text='发送远程通知'
+            text='Request Remote Notification'
             elevation={2}
             onClick={() => this.remoteNotification()}
             />
         </View>
+        { utils.isNotEmpty(this.state.notifyToken) &&
+          <View style={styles.text}>
+            <Button
+              text='Update Firebase Token'
+              elevation={2}
+              onClick={() => this.updateToken()}
+            />
+          </View>
+        }
         <View style={[ui.flex1, ui.flexcc]}>
           <Text style={styles.os}>{Platform.OS}</Text>
         </View>

+ 0 - 21
Strides-APP/app/pages/sign/Login.js

@@ -30,27 +30,6 @@ export const AutoLogin = async (back) => {
   }
 }
 
-//注册FCM通知token
-export const RegisterToken = async () => {
-  if (getUserId() > 0 && notifyToken.token) {
-    const thisDate = utils.formatYYMM(new Date()) + "-" + getUserId();
-    const lastDate = await getStorageSync('RegisterTokenDate');
-    console.log('>>>RegisterToken<<<', thisDate, lastDate + "●");
-    if (thisDate != lastDate) {
-      const params = {
-        os: notifyToken.os ? notifyToken.os : Platform.OS,
-        googleToken: notifyToken.token
-      }
-      apiUser.setNotifyToken(params).then(res => {
-        console.log('>>>RegisterToken-Suc<<<', res);
-        setStorage('RegisterTokenDate', thisDate);
-      }).catch(err => {
-        console.log('>>>RegisterToken-Err<<<', err);
-      });
-    }
-  }
-}
-
 export default class Login extends React.Component {
 
   constructor(props) {

+ 1 - 23
Strides-APP/app/pages/sign/LoginV2.js

@@ -7,7 +7,7 @@ import {View, Text, StyleSheet, Image, TextInput, ScrollView, Platform} from 're
 import { BackButton } from '../../components/Toolbar';
 import apiUser from '../../api/apiUser';
 import { setAccessToken } from '../../api/http';
-import { getStorageSync, setStorage, getStorageJsonSync, setStorageJson } from '../../utils/storage';
+import { getStorageJsonSync, setStorageJson } from '../../utils/storage';
 import Button from '../../components/Button';
 import Dialog from '../../components/Dialog';
 import { PageList } from '../Router';
@@ -29,29 +29,7 @@ export const AutoLogin = async (back) => {
   }
 }
 
-//注册FCM通知token
-export const RegisterToken = async () => {
-  if (getUserId() > 0 && notifyToken.token) {
-    const thisDate = utils.formatYYMM(new Date()) + "-" + getUserId();
-    const lastDate = await getStorageSync('RegisterTokenDate');
-    console.log('>>>RegisterToken<<<', thisDate, lastDate + "●");
-    if (thisDate != lastDate) {
-      const params = {
-        os: notifyToken.os ? notifyToken.os : Platform.OS,
-        googleToken: notifyToken.token
-      }
-      apiUser.setNotifyToken(params).then(res => {
-        console.log('>>>RegisterToken-Suc<<<', res);
-        setStorage('RegisterTokenDate', thisDate);
-      }).catch(err => {
-        console.log('>>>RegisterToken-Err<<<', err);
-      });
-    }
-  }
-}
-
 export default class Login extends React.Component {
-
   constructor(props) {
     super(props);
     this.state = {

+ 28 - 0
Strides-APP/app/utils/utils.js

@@ -1,5 +1,7 @@
 import { Linking } from "react-native";
 import { showLocation } from 'react-native-map-link'
+import apiUser from "../api/apiUser";
+import { getStorageSync, setStorage } from "./storage";
 
 /**
  * 工具集
@@ -186,5 +188,31 @@ export default {
     } else {
       return text;
     }
+  },
+  /**
+   * 注册FCM通知token
+   * @param {String} token Firebase令牌
+   * @param {Function} back 执行结果(boolean)
+   */
+  async registerFirebaseToken(token, back) {
+    const thisDate = this.formatYYMM(new Date()) + "-" + getUserId();
+    const lastDate = await getStorageSync('RegisterTokenDate');
+    console.log('>>>RegisterToken<<<', thisDate, lastDate + "●");
+    if (thisDate != lastDate || token != notifyToken.token) {
+      const params = {
+        os: isIOS ? "ios": "android",
+        googleToken: notifyToken.token
+      }
+      apiUser.setNotifyToken(params).then(res => {
+        console.log('>>>RegisterToken-Suc<<<', res);
+        setStorage('RegisterTokenDate', thisDate);
+        if (back) back(true);
+      }).catch(err => {
+        console.log('>>>RegisterToken-Err<<<', err);
+        if (back) back(false);
+      });
+    } else {
+      if (back) back(false);
+    }
   }
 }

+ 7 - 7
Strides-APP/ios/JuicePlus/GoogleService-Info.plist

@@ -3,21 +3,21 @@
 <plist version="1.0">
 <dict>
 	<key>CLIENT_ID</key>
-	<string>779803805444-7vilfstvrrjvolvc3mgq5iklpied7f5e.apps.googleusercontent.com</string>
+	<string>261971604133-8fsde5cle4nhs3mjvs6nm99cdq87ij52.apps.googleusercontent.com</string>
 	<key>REVERSED_CLIENT_ID</key>
-	<string>com.googleusercontent.apps.779803805444-7vilfstvrrjvolvc3mgq5iklpied7f5e</string>
+	<string>com.googleusercontent.apps.261971604133-8fsde5cle4nhs3mjvs6nm99cdq87ij52</string>
 	<key>API_KEY</key>
-	<string>AIzaSyC18ciIi5QnMpCphuLPyXfwasz4C7YqkKU</string>
+	<string>AIzaSyAqZzeyG4YRY1sHyGPYVoWRE2nTIRvsKbU</string>
 	<key>GCM_SENDER_ID</key>
-	<string>779803805444</string>
+	<string>261971604133</string>
 	<key>PLIST_VERSION</key>
 	<string>1</string>
 	<key>BUNDLE_ID</key>
 	<string>com.strides.chargeco</string>
 	<key>PROJECT_ID</key>
-	<string>strides-67073</string>
+	<string>chargeco-1d845</string>
 	<key>STORAGE_BUCKET</key>
-	<string>strides-67073.appspot.com</string>
+	<string>chargeco-1d845.appspot.com</string>
 	<key>IS_ADS_ENABLED</key>
 	<false></false>
 	<key>IS_ANALYTICS_ENABLED</key>
@@ -29,6 +29,6 @@
 	<key>IS_SIGNIN_ENABLED</key>
 	<true></true>
 	<key>GOOGLE_APP_ID</key>
-	<string>1:779803805444:ios:c6bd33a7368d3fe07af251</string>
+	<string>1:261971604133:ios:262e330b5a879efec8134c</string>
 </dict>
 </plist>