Skip to content

Commit

Permalink
Send registration fail without PlayServices
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Jan 18, 2024
1 parent 10ebae5 commit af28634
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.util.Log
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.removeToken
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.saveToken
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.sendRegistrationFailed

private const val TAG = "UP-Embedded_distributor"

Expand All @@ -23,6 +25,9 @@ open class EmbeddedDistributorReceiver : BroadcastReceiver() {
when (intent.action) {
ACTION_REGISTER -> {
Log.d(TAG, "Registering to the embedded distributor")
if (!isPlayServicesAvailable(context)) {
sendRegistrationFailed(context, token, "PlayServices not available")
}
saveGetEndpoint(context)
saveToken(context, token)
registerFCM(context)
Expand All @@ -39,6 +44,18 @@ open class EmbeddedDistributorReceiver : BroadcastReceiver() {
}
}

private fun isPlayServicesAvailable(context: Context): Boolean {
val pm = context.packageManager
try {
pm.getPackageInfo("com.google.android.gms", PackageManager.GET_ACTIVITIES)
return true

} catch (e: PackageManager.NameNotFoundException) {
Log.v(TAG, e.message!!)
}
return false
}

private fun registerFCM(context: Context) {
val intent = Intent(ACTION_FCM_TOKEN_REQUEST)
intent.setPackage(GSF_PACKAGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ object Utils {
context.sendBroadcast(broadcastIntent)
}

fun sendRegistrationFailed(
context: Context,
connectionToken: String,
message: String,
) {
val broadcastIntent = Intent()
broadcastIntent.`package` = context.packageName
broadcastIntent.action = ACTION_REGISTRATION_FAILED
broadcastIntent.putExtra(EXTRA_TOKEN, connectionToken)
broadcastIntent.putExtra(EXTRA_MESSAGE, message)
context.sendBroadcast(broadcastIntent)
}

private fun getEndpoint(context: Context, fcmToken: String, instance: String): String? {
val prefs = context.getSharedPreferences(PREF_MASTER, Context.MODE_PRIVATE)
val ff = 0xff.toChar().toString()
Expand Down

0 comments on commit af28634

Please sign in to comment.