-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from horaciocome1/configure-firebase-cloud-mes…
…saging configure-firebase-cloud-messaging
- Loading branch information
Showing
8 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/io/github/horaciocome1/reaque/services/MyFirebaseMessagingService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters