Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package acr.browser.lightning.fcm
  2.  
  3. import acr.browser.lightning.MainActivity
  4. import acr.browser.lightning.R
  5. import android.app.NotificationChannel
  6. import android.app.NotificationManager
  7. import android.app.PendingIntent
  8. import android.content.Context
  9. import android.content.Intent
  10. import android.media.RingtoneManager
  11. import android.os.Build
  12. import androidx.core.app.NotificationCompat
  13. import android.util.Log
  14. import com.google.firebase.messaging.FirebaseMessagingService
  15. import com.google.firebase.messaging.RemoteMessage
  16.  
  17. class MyFirebaseMessagingService : FirebaseMessagingService() {
  18.  
  19. override fun onMessageReceived(remoteMessage: RemoteMessage) {
  20. Log.d(TAG, "From: ${remoteMessage?.from}")
  21.  
  22. remoteMessage?.notification?.let {
  23. Log.d(TAG, "Message Notification Body: ${it.body}")
  24. sendNotification(it.title, it.body)
  25. }
  26.  
  27. }
  28.  
  29. override fun onNewToken(token: String) {
  30. Log.d(TAG, "Refreshed token: $token")
  31. sendRegistrationToServer(token)
  32. }
  33.  
  34. private fun handleNow() {
  35. Log.d(TAG, "Short lived task is done.")
  36. }
  37.  
  38. private fun sendRegistrationToServer(token: String?) {
  39. // TODO: Implement this method to send token to your app server.
  40. }
  41.  
  42. private fun sendNotification(title: String?, body: String?) {
  43. val intent = Intent(this, MainActivity::class.java)
  44. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
  45. val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  46. PendingIntent.FLAG_ONE_SHOT)
  47.  
  48. val channelId = getString(R.string.default_notification_channel_id)
  49. val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
  50. val notificationBuilder = NotificationCompat.Builder(this, channelId)
  51. .setSmallIcon(R.drawable.ic_message_white_24dp)
  52. .setContentTitle(title)
  53. .setContentText(body)
  54. .setAutoCancel(true)
  55. .setSound(defaultSoundUri)
  56. .setContentIntent(pendingIntent)
  57.  
  58. val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  59.  
  60. // Since android Oreo notification channel is needed.
  61. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  62. val channel = NotificationChannel(channelId,
  63. "Channel human readable title",
  64. NotificationManager.IMPORTANCE_DEFAULT)
  65. notificationManager.createNotificationChannel(channel)
  66. }
  67.  
  68. notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
  69. }
  70.  
  71. companion object {
  72.  
  73. private const val TAG = "MyFirebaseMsgService"
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement