Skip to content

Commit 16604cc

Browse files
author
Gowtham
committed
sending push to multiple tokens is single call added
1 parent ae9ced7 commit 16604cc

File tree

14 files changed

+43
-7640
lines changed

14 files changed

+43
-7640
lines changed

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/fcmsender/fcm_sender_kotlin/MainActivity.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class MainActivity : AppCompatActivity(), FCMSender.ResponseListener {
2323
data.put("message", "Click to view..")
2424
data.put("type", "new_logged_in")
2525
val push = FCMSender.Builder()
26-
.serverKey("Server key")
27-
.to("topic/testTopic") //either topic or user registration token
26+
.serverKey("Your server key")
27+
.toTokenOrTopic("topic/TestTopic") //either topic or user registration token
28+
// .toMultipleTokens(listOfToken)
2829
.responseListener(this)
2930
// .setTimeToLive(30) // 0 to 2,419,200 seconds (4 weeks)
3031
// .setDryRun(false) //test a request without actually sending a message.
@@ -38,8 +39,8 @@ class MainActivity : AppCompatActivity(), FCMSender.ResponseListener {
3839
Toast.makeText(this, "onSuccess", Toast.LENGTH_SHORT).show()
3940
}
4041

41-
override fun onFailure(errorCode: Int) {
42-
Log.d("onFailure","s$errorCode")
42+
override fun onFailure(errorCode: Int,message: String) {
43+
Log.d("onFailure","$errorCode $message")
4344
Toast.makeText(this, "onFailure", Toast.LENGTH_SHORT).show()
4445
}
4546

hs_err_pid19049.log

Lines changed: 0 additions & 955 deletions
This file was deleted.

hs_err_pid19109.log

Lines changed: 0 additions & 950 deletions
This file was deleted.

hs_err_pid19234.log

Lines changed: 0 additions & 956 deletions
This file was deleted.

hs_err_pid19351.log

Lines changed: 0 additions & 950 deletions
This file was deleted.

hs_err_pid19500.log

Lines changed: 0 additions & 950 deletions
This file was deleted.

hs_err_pid19550.log

Lines changed: 0 additions & 952 deletions
This file was deleted.

hs_err_pid19630.log

Lines changed: 0 additions & 953 deletions
This file was deleted.

hs_err_pid19684.log

Lines changed: 0 additions & 950 deletions
This file was deleted.

library/src/main/java/com/fcmsender/ApiCall.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.coroutines.launch
99
import okhttp3.*
1010
import okhttp3.MediaType.Companion.toMediaTypeOrNull
1111
import okhttp3.RequestBody.Companion.toRequestBody
12+
import org.json.JSONArray
1213
import org.json.JSONObject
1314
import java.io.IOException
1415
import java.util.concurrent.TimeUnit
@@ -19,26 +20,29 @@ internal class ApiCall(val context: Context) {
1920

2021
private var responseListener: FCMSender.ResponseListener? = null
2122

22-
private var to: String? = null
23+
private var toTokenOrTopic: String? = null
2324

2425
private var timeToLive: Long?=null
2526

2627
private var dryRun: Boolean?=null
2728

29+
private var toMultipleToken: ArrayList<String>?=null
30+
2831
private var customData: JSONObject? = null
2932

3033
constructor(
3134
context: Context,
3235
serverKey: String,
3336
responseListener: FCMSender.ResponseListener,
34-
to: String?,
37+
toTokenOrTopic: String?,
38+
toMultipleToken: ArrayList<String>?,
3539
timeToLive: Long?,
3640
dryRun: Boolean?,
37-
customData: JSONObject?
38-
) : this(context) {
41+
customData: JSONObject?) : this(context) {
3942
this.serverKey = serverKey
4043
this.responseListener = responseListener
41-
this.to = to
44+
this.toTokenOrTopic = toTokenOrTopic
45+
this.toMultipleToken = toMultipleToken
4246
this.timeToLive = timeToLive
4347
this.dryRun = dryRun
4448
this.customData = customData
@@ -47,7 +51,10 @@ internal class ApiCall(val context: Context) {
4751
fun sendPush() {
4852
try {
4953
val jsonObject = JSONObject()
50-
jsonObject.put("to", to)
54+
if (toTokenOrTopic!=null)
55+
jsonObject.put("to", toTokenOrTopic)
56+
else
57+
jsonObject.put("registration_ids", JSONArray(toMultipleToken))
5158
jsonObject.put("priority","high")
5259
timeToLive?.let { jsonObject.put("time_to_live",it) }
5360
dryRun?.let { jsonObject.put("dry_run",it) }
@@ -76,7 +83,7 @@ internal class ApiCall(val context: Context) {
7683
override fun onFailure(call: Call, e: IOException) {
7784
e.printStackTrace()
7885
GlobalScope.launch(Dispatchers.Main) {
79-
responseListener?.onFailure(188)
86+
responseListener?.onFailure(188,e.message.toString())
8087
}
8188
}
8289

@@ -85,7 +92,7 @@ internal class ApiCall(val context: Context) {
8592
if(response.code==200)
8693
responseListener?.onSuccess(response.body?.string().toString())
8794
else
88-
responseListener?.onFailure(response.code)
95+
responseListener?.onFailure(response.code,response.body?.string().toString())
8996
}
9097
}
9198
})

library/src/main/java/com/fcmsender/FCMSender.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import org.json.JSONObject
55

66
class FCMSender(
77
private val serverKey: String?,
8-
private val to: String?,
8+
private val toTokenOrTopic: String?,
9+
private val toMultipleToken: ArrayList<String>?,
910
private val timeToLive: Long?,
1011
private val dryRun: Boolean?,
1112
private val customData: JSONObject?,
12-
private val listener: ResponseListener?
13-
) {
13+
private val listener: ResponseListener?) {
1414

1515
interface ResponseListener {
1616
fun onSuccess(response: String)
17-
fun onFailure(errorCode: Int)
17+
fun onFailure(errorCode: Int,message: String)
1818
}
1919

2020
fun sendPush(context: Context) {
@@ -24,12 +24,14 @@ class FCMSender(
2424
throw NullPointerException("Serverkey is null")
2525
}
2626
listener == null -> throw NullPointerException("Response Listener is null")
27-
to == null -> throw NullPointerException("to is null")
27+
toTokenOrTopic == null && toMultipleToken==null -> throw NullPointerException("toTokenOrTopic and toMultipleToken is null.You can use either one")
28+
toTokenOrTopic != null && toMultipleToken!=null -> throw IllegalArgumentException("You've passed toTokenOrTopic and toMultipleToken.You can use either one")
29+
toMultipleToken != null && toMultipleToken.isEmpty() -> throw IllegalArgumentException("You've passed a empty list for toMultipleToken")
2830
customData == null -> throw NullPointerException("data is null")
2931
else -> {
3032
val apiCall = ApiCall(
3133
context, serverKey, listener,
32-
to, timeToLive, dryRun, customData
34+
toTokenOrTopic,toMultipleToken, timeToLive, dryRun, customData
3335
)
3436
apiCall.sendPush()
3537
}
@@ -42,18 +44,20 @@ class FCMSender(
4244
data class Builder(
4345
private var serverKey: String? = null,
4446
private var listener: ResponseListener? = null,
45-
private var to: String? = null,
47+
private var toTokenOrTopic: String? = null,
48+
private var toMultipleTokens: ArrayList<String>?=null,
4649
private var timeToLive: Long? = null,
4750
private var dryRun: Boolean=false,
48-
private var customData: JSONObject? = null
49-
) {
51+
private var customData: JSONObject? = null) {
52+
5053
fun serverKey(serverKey: String) = apply { this.serverKey = serverKey }
51-
fun to(to: String) = apply { this.to = to }
54+
fun toTokenOrTopic(to: String) = apply { this.toTokenOrTopic = to }
55+
fun toMultipleTokens(tokens: ArrayList<String>) = apply { this.toMultipleTokens = tokens }
5256
fun setTimeToLive(timeToLive: Long) = apply { this.timeToLive = timeToLive }
5357
fun setDryRun(enable: Boolean) = apply { this.dryRun = enable }
5458
fun setData(data: JSONObject) = apply { this.customData = data }
5559
fun responseListener(listener: ResponseListener?) = apply { this.listener = listener }
5660
fun build() =
57-
FCMSender(serverKey, to, timeToLive, dryRun, customData, listener)
61+
FCMSender(serverKey, toTokenOrTopic,toMultipleTokens, timeToLive, dryRun, customData, listener)
5862
}
5963
}

0 commit comments

Comments
 (0)