Skip to content

Commit

Permalink
Merge pull request #41 from horaciocome1/configure-firebase-cloud-mes…
Browse files Browse the repository at this point in the history
…saging

configure-firebase-cloud-messaging
  • Loading branch information
horaciocome1 authored Aug 8, 2019
2 parents ac038e1 + c305de9 commit 6860aad
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ android {
applicationId "io.github.horaciocome1.reaque"
minSdkVersion 16
targetSdkVersion 29
versionCode 9
versionName "1.2.0-rc-1"
versionCode 10
versionName "1.3.0-rc-1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
Expand Down Expand Up @@ -87,6 +87,7 @@ dependencies {
implementation 'com.google.firebase:firebase-perf:18.0.1'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.google.firebase:firebase-dynamic-links:18.0.0'
implementation 'com.google.firebase:firebase-messaging:19.0.1'

// play services
implementation 'com.google.android.gms:play-services-auth:17.0.0'
Expand Down
27 changes: 24 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="reaque.page.link"
android:scheme="https" />
Expand All @@ -54,11 +51,35 @@
android:scheme="http" />
</intent-filter>

<intent-filter>
<action android:name="MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

</activity>

<service android:name=".services.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/secondaryColor" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ class UsersService : UsersInterface {
return users
}

fun updateRegistrationToken(token: String, onCompleteListener: (Task<Void?>?) -> Unit) {
if (token.isNotBlank())
auth.addSimpleAuthStateListener {
val data = mapOf("registrationToken" to token)
val ref = db.document("users/${it.uid}")
ref.set(data, SetOptions.merge()).addOnCompleteListener(onCompleteListener)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.github.horaciocome1.reaque.services

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.github.horaciocome1.reaque.R
import io.github.horaciocome1.reaque.data.users.UsersService
import io.github.horaciocome1.reaque.ui.MainActivity
import io.github.horaciocome1.reaque.util.Constants.MAIN_ACTIVITY
import io.github.horaciocome1.reaque.util.Constants.USER_ID

class MyFirebaseMessagingService : FirebaseMessagingService() {

override fun onNewToken(token: String?) {
val service = UsersService()
token?.let { service.updateRegistrationToken(it) {} }
}

override fun onMessageReceived(remoteMessage: RemoteMessage?) {
remoteMessage?.let {
if (it.data.isNotEmpty() && it.notification != null) {
val userId = it.data[USER_ID]
val title = it.notification!!.title
val body = it.notification!!.body
val clickAction = it.notification!!.clickAction
if (userId != null && title != null && body != null && clickAction != null)
if (userId.isNotBlank() && title.isNotBlank() && body.isNotBlank() && clickAction.isNotBlank())
sendNotification(title, body, clickAction, userId)
}
}
}

private fun sendNotification(title: String, body: String, clickAction: String, userId: String) {
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
if (clickAction == MAIN_ACTIVITY)
intent.putExtra(USER_ID, userId)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification =
NotificationCompat.Builder(this, resources.getString(R.string.default_notification_channel_id))
.apply {
setSmallIcon(R.mipmap.ic_launcher)
setContentTitle(title)
setContentText(body)
setAutoCancel(true)
setSound(sound)
setContentIntent(pendingIntent)
}
.build()
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
manager?.notify(0, notification)
}

}
29 changes: 29 additions & 0 deletions app/src/main/java/io/github/horaciocome1/reaque/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.firebase.auth.FirebaseAuth
import io.github.horaciocome1.reaque.R
import io.github.horaciocome1.reaque.data.users.User
import io.github.horaciocome1.reaque.util.Constants.USER_ID
import io.github.horaciocome1.reaque.util.handleDynamicLinks
import kotlinx.android.synthetic.main.activity_main.*

Expand All @@ -42,13 +46,20 @@ class MainActivity : AppCompatActivity() {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkGoogleApiAvailability()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
setSupportActionBar(toolbar)
supportActionBar?.setDisplayShowTitleEnabled(false)
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
auth = FirebaseAuth.getInstance()
setupNavigation()
handleNotifications {
val bundle = Bundle().apply {
putString("user_id", it.id)
}
navController.navigate(R.id.destination_user_profile, bundle)
}
handleDynamicLinks {
val bundle = Bundle().apply {
putString("post_id", it.id)
Expand All @@ -57,8 +68,19 @@ class MainActivity : AppCompatActivity() {
}
}

private fun handleNotifications(listener: (User) -> Unit) {
val userId = intent.getStringExtra(USER_ID)
userId?.let {
if (it.isNotBlank()) {
val user = User(it)
listener(user)
}
}
}

override fun onStart() {
super.onStart()
checkGoogleApiAvailability()
if (auth.currentUser == null)
navController.navigate(R.id.destination_sign_in)
}
Expand Down Expand Up @@ -90,6 +112,13 @@ class MainActivity : AppCompatActivity() {
navController.addOnDestinationChangedListener(onDestinationChangedListener)
}

private fun checkGoogleApiAvailability() {
val instance = GoogleApiAvailability.getInstance()
val code = instance.isGooglePlayServicesAvailable(this)
if (code != ConnectionResult.SUCCESS)
instance.makeGooglePlayServicesAvailable(this)
}

private val isOrientationPortrait: Boolean
get() {
return resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ object Constants {
const val TOPIC_POSTS_REQUEST = "topic_posts"
const val TOPIC_USERS_REQUEST = "topic_users"
const val USER_POSTS_REQUEST = "user_posts"
const val USER_ID = "USER_ID"
const val MAIN_ACTIVITY = "MainActivity"

}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

<string name="app_name" translatable="false">Reaque</string>

<string name="default_notification_channel_id" translatable="false">reaque-firebase-messaging-channel_id</string>

<!-- 01 URL -->
<string name="url_terms_and_conditions" translatable="false">https://github.com/horaciocome1/reaque/tree/master/docs/TERMS_AND_CONDITIONS.md</string>
<string name="url_privacy_policy" translatable="false">https://github.com/horaciocome1/reaque/tree/master/docs/PRIVACY_POLICY.md</string>
Expand Down

0 comments on commit 6860aad

Please sign in to comment.