Skip to content

Commit 34ec793

Browse files
committed
Fix code for sending fcm
1 parent 84dfd11 commit 34ec793

9 files changed

Lines changed: 80 additions & 12 deletions

File tree

android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ plugins {
66
}
77

88
android {
9-
compileSdk 32
9+
compileSdk 31
1010

1111
buildToolsVersion "32.0.0"
1212

1313
defaultConfig {
1414
applicationId "com.httpsms"
1515
minSdk 30
16-
targetSdk 32
16+
targetSdk 30
1717
versionCode 1
1818
versionName "1.0"
1919

android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
6262

6363
internal class SendSmsWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
6464
override fun doWork(): Result {
65+
if (!Settings.isLoggedIn(applicationContext)) {
66+
Timber.w("user is not logged in, stopping processing")
67+
return Result.failure()
68+
}
69+
6570
val owner = Settings.getOwner(applicationContext) ?: return Result.failure()
6671
val message = getMessage(applicationContext, owner) ?: return Result.failure()
6772

android/app/src/main/java/com/httpsms/HttpSmsApiService.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ class HttpSmsApiService(private val apiKey: String) {
9696
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'000000'ZZZZZ")
9797
val timestampString = formatter.format(timestamp).replace("+", "Z")
9898

99+
var reasonString = "null"
100+
if (reason != null) {
101+
reasonString = "\"$reason\""
102+
}
103+
99104
val body = """
100105
{
101106
"event_name": "$event",
102-
"reason": "$reason"
107+
"reason": $reasonString,
103108
"timestamp": "$timestampString"
104109
}
105110
""".trimIndent()

android/app/src/main/java/com/httpsms/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class MainActivity : AppCompatActivity() {
5353
}
5454

5555
private fun refreshToken() {
56-
56+
Timber.e("FCM TOKEN: ${Settings.getFcmToken(this)}")
5757
}
5858

5959
private fun initTimber() {

android/app/src/main/java/com/httpsms/Models.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@ data class Message (
3737
val createdAt: String,
3838

3939
@Json(name = "failure_reason")
40-
val failureReason: String,
40+
val failureReason: String?,
4141

4242
val id: String,
4343

4444
@Json(name = "last_attempted_at")
45-
val lastAttemptedAt: String,
45+
val lastAttemptedAt: String?,
4646

4747
@Json(name = "order_timestamp")
4848
val orderTimestamp: String,
4949

5050
val owner: String,
5151

5252
@Json(name = "received_at")
53-
val receivedAt: String,
53+
val receivedAt: String?,
5454

5555
@Json(name = "request_received_at")
5656
val requestReceivedAt: String,
5757

5858
@Json(name = "send_time")
59-
val sendTime: Long,
59+
val sendTime: Long?,
6060

6161
@Json(name = "sent_at")
62-
val sentAt: String,
62+
val sentAt: String?,
6363

6464
val status: String,
6565
val type: String,

android/app/src/main/java/com/httpsms/ReceivedReceiver.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class ReceivedReceiver: BroadcastReceiver()
3434

3535
private fun handleMessageReceived(context: Context, from: String, to : String, content: String) {
3636
val timestamp = ZonedDateTime.now(ZoneOffset.UTC)
37+
38+
if (!Settings.isLoggedIn(context)) {
39+
Timber.w("user is not logged in")
40+
return
41+
}
42+
3743
Thread {
3844
Timber.i("forwarding received message from [${from}]")
3945
HttpSmsApiService(Settings.getApiKeyOrDefault(context)).receive(from, to, content, timestamp)

android/app/src/main/java/com/httpsms/Receiver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ object Receiver {
1010
return false
1111
}
1212

13-
if (Settings.isLoggedIn(context)) {
13+
if (!Settings.isLoggedIn(context)) {
1414
Timber.w("cannot handle message with id [$messageId] because the user is not logged in")
1515
return false
1616
}
1717
return true
1818
}
19-
}
19+
}

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
}
88
dependencies {
99
// Add this line
10-
classpath 'com.google.gms:google-services:4.3.12'
10+
classpath 'com.google.gms:google-services:4.3.13'
1111
}
1212
}
1313

api/cmd/fcm/main.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package main
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/json"
57
"fmt"
8+
"io/ioutil"
69
"log"
10+
"math/rand"
11+
"net/http"
712
"os"
13+
"time"
14+
15+
"github.com/davecgh/go-spew/spew"
816

917
"firebase.google.com/go/messaging"
1018
"github.com/joho/godotenv"
@@ -31,6 +39,11 @@ func main() {
3139
log.Fatal(stacktrace.Propagate(err, "cannot create messaging client"))
3240
}
3341

42+
err = createSmsMessage()
43+
if err != nil {
44+
log.Fatal(stacktrace.Propagate(err, "cannot create sms message"))
45+
}
46+
3447
result, err := client.Send(context.Background(), &messaging.Message{
3548
Data: map[string]string{
3649
"hello": "world",
@@ -43,3 +56,42 @@ func main() {
4356

4457
log.Println(fmt.Sprintf("sent event with response [%s]", result))
4558
}
59+
60+
func createSmsMessage() error {
61+
payload, err := json.Marshal(map[string]string{
62+
"from": os.Getenv("PHONE_NUMBER"),
63+
"to": os.Getenv("PHONE_NUMBER"),
64+
"content": fmt.Sprintf("[%s] random message [%d]", time.Now().String(), rand.Int()),
65+
})
66+
if err != nil {
67+
return stacktrace.Propagate(err, "cannot convert message to map")
68+
}
69+
70+
client := http.Client{}
71+
req, err := http.NewRequest(http.MethodPost, "https://api.httpsms.com/v1/messages/send", bytes.NewBuffer(payload))
72+
if err != nil {
73+
return stacktrace.Propagate(err, "cannot do http request")
74+
}
75+
76+
req.Header = http.Header{
77+
"Content-Type": {"application/json"},
78+
"X-API-Key": {os.Getenv("API_KEY")},
79+
}
80+
81+
response, err := client.Do(req)
82+
if err != nil {
83+
return stacktrace.Propagate(err, "cannot do http request")
84+
}
85+
86+
body, err := ioutil.ReadAll(response.Body)
87+
if err != nil {
88+
return stacktrace.Propagate(err, "cannot read response body")
89+
}
90+
91+
if response.StatusCode >= 400 {
92+
return stacktrace.NewError(fmt.Sprintf("[%s] %s", response.StatusCode, string(body)))
93+
}
94+
95+
spew.Dump(string(body))
96+
return nil
97+
}

0 commit comments

Comments
 (0)