Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The installation guide might be slightly off... #12

Open
J-Cake opened this issue Mar 9, 2019 · 1 comment
Open

The installation guide might be slightly off... #12

J-Cake opened this issue Mar 9, 2019 · 1 comment

Comments

@J-Cake
Copy link

J-Cake commented Mar 9, 2019

Can I just say that while this module is bloody brilliant, I've had a hard time installing it. I'd like to know whether you actually meant to add the stuff to MainActivity.java or to MainApplication.java because the latter contains a function called OnCreate, the prior doesn't.

@fc
Copy link
Contributor

fc commented Apr 16, 2019

Yeah it's broken, until #15 is merged it won't work.

You can use my branch though:

yarn add "fc/react-native-android-sms#fc/react-native-compat"

The install instructions are also wrong which I did not address in the PR/README but can be understood below in this patch:

diff --git a/android/app/build.gradle b/android/app/build.gradle
index 4fa84e8..71de8de 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -106,6 +106,7 @@ apply from: 'expo.gradle'
 dependencies {
     compile project(':react-native-call-log')
   implementation fileTree(dir: 'libs', include: ['*.jar'])
+    compile project(':react-native-android-sms')
 
   implementation 'com.android.support:multidex:1.0.1'
 
diff --git a/android/app/src/main/java/host/exp/exponent/MainApplication.java b/android/app/src/main/java/host/exp/exponent/MainApplication.java
index af14912..48b3dd2 100644
--- a/android/app/src/main/java/host/exp/exponent/MainApplication.java
+++ b/android/app/src/main/java/host/exp/exponent/MainApplication.java
@@ -31,6 +31,7 @@ import expo.modules.sensors.SensorsPackage;
 import expo.modules.sms.SMSPackage;
 import expo.modules.taskManager.TaskManagerPackage;
 import expolib_v1.okhttp3.OkHttpClient;
+import my.qash.react.SmsPackage;
 
 // Needed for `react-native link`
 // import com.facebook.react.ReactApplication;
@@ -54,7 +55,8 @@ public class MainApplication extends ExpoApplication implements AppLoaderPackage
 
         // Needed for `react-native link`
         // new MainReactPackage(),
-            new CallLogPackage()
+            new CallLogPackage(),
+            new SmsPackage()
     );
   }
 
diff --git a/android/settings.gradle b/android/settings.gradle
index 5f52356..c48799f 100644
--- a/android/settings.gradle
+++ b/android/settings.gradle
@@ -1,3 +1,5 @@
 include ':app'
 include ':react-native-call-log'
 project(':react-native-call-log').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-call-log/android')
+include ':react-native-android-sms'
+project(':react-native-android-sms').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-sms')
diff --git a/package.json b/package.json
index 7554430..a133af3 100644
--- a/package.json
+++ b/package.json
@@ -16,6 +16,7 @@
     "expokit": "32.1.1",
     "react": "16.5.0",
     "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
+    "react-native-android-sms": "fc/react-native-android-sms#fc/react-native-compat",
     "react-native-call-log": "^2.1.1",
     "react-navigation": "^3.0.9"
   },
diff --git a/screens/HomeScreen.js b/screens/HomeScreen.js
index ad650e4..197db7d 100644
--- a/screens/HomeScreen.js
+++ b/screens/HomeScreen.js
@@ -11,6 +11,7 @@ import {
 } from "react-native";
 import { WebBrowser, Contacts, Permissions } from "expo";
 import CallLogs from "react-native-call-log";
+import SmsAndroid from "react-native-android-sms";
 
 import { MonoText } from "../components/StyledText";
 
@@ -56,6 +57,49 @@ export default class HomeScreen extends React.Component {
         console.log(contacts, 5)
       );
     }
+
+    const grantedReadSms = await PermissionsAndroid.request(
+      PermissionsAndroid.PERMISSIONS.READ_SMS,
+      {
+        title: "Read SMS Example",
+        message: "Access your SMS logs",
+        buttonNeutral: "Ask Me Later",
+        buttonNegative: "Cancel",
+        buttonPositive: "OK"
+      }
+    );
+
+    if (grantedReadSms === PermissionsAndroid.RESULTS.GRANTED) {
+      const filter = {
+        box: "inbox", // 'inbox' (default), 'sent', 'draft', 'outbox', 'failed', 'queued', and '' for all
+        // the next 4 filters should NOT be used together, they are OR-ed so pick one
+        read: 0, // 0 for unread SMS, 1 for SMS already read
+        //_id: 1234, // specify the msg id
+        //address: '+97433------', // sender's phone number
+        //body: 'Hello', // content to match
+        // the next 2 filters can be used for pagination
+        indexFrom: 0, // start from index 0
+        maxCount: 10 // count of SMS to return each time
+      };
+
+      SmsAndroid.list(
+        JSON.stringify(filter),
+        fail => {
+          console.log("OH Snap: " + fail);
+        },
+        (count, smsList) => {
+          console.log("Count: ", count);
+          console.log("List: ", smsList);
+          var arr = JSON.parse(smsList);
+          for (var i = 0; i < arr.length; i++) {
+            var obj = arr[i];
+            console.log("Index: " + i);
+            console.log("-->" + obj.date);
+            console.log("-->" + obj.body);
+          }
+        }
+      );
+    }
   };
 
   // async componentDidlMount() {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants