Advertisement
kobylynskyiv

Untitled

Nov 17th, 2020 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.68 KB | None | 0 0
  1. class MyFirebaseMessagingService() : FirebaseMessagingService(){
  2.  
  3.     @SuppressLint("LongLogTag")
  4.     override fun onMessageReceived(remoteMessage: RemoteMessage) {
  5.         Log.e("MyFirebaseMessagingService", "onMessageReceived")
  6.         val currentToken =    getSharedPreferences("prefs", Context.MODE_PRIVATE).getString(
  7.             "current_token",
  8.             null
  9.         )
  10.        if(remoteMessage.data.isNotEmpty() && currentToken == remoteMessage.to){
  11.            val notificationName = remoteMessage.data["notificationName"]
  12.            sendNotification(notificationName)
  13.        }
  14.     }
  15.  
  16.     @SuppressLint("LongLogTag")
  17.     override fun onDeletedMessages() {
  18.         Log.e("MyFirebaseMessagingService", "onDeletedMessages")
  19.         super.onDeletedMessages()
  20.     }
  21.  
  22.     @SuppressLint("LongLogTag")
  23.     override fun onNewToken(token: String) {
  24.         println("yeah")
  25.         Log.e("MyFirebaseMessagingService", "onNewToken")
  26.         sendToken(token)
  27.         getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().putString("current_token", token).apply()
  28.     }
  29.  
  30.     @SuppressLint("LongLogTag")
  31.     private fun sendToken(token: String){
  32.         Log.e("MyFirebaseMessagingService", "sendToken")
  33.         var urlConnection: HttpURLConnection? = null
  34.         try {
  35.             val  url = URL("https://${BASE_URL}/Account/push/$token")
  36.             urlConnection = url.openConnection() as HttpURLConnection
  37.         } catch (e: Exception) {
  38.             e.printStackTrace()
  39.         } finally {
  40.             urlConnection?.disconnect()
  41.         }
  42.     }
  43.  
  44.  
  45.     @SuppressLint("LongLogTag")
  46.     fun sendNotification(notificationName: String?) {
  47.         Log.e("MyFirebaseMessagingService", "sendNotification")
  48.         val text = getNotificationText(notificationName)
  49.         if(text!=null){
  50.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  51.                 sendOreoNotification(text)
  52.             } else {
  53.                 sendDefaultNotification(text)
  54.             }
  55.         }
  56.     }
  57.  
  58.     @RequiresApi(api = Build.VERSION_CODES.O)
  59.     private fun sendOreoNotification(text: String) {
  60.         val notification = OreoNotification(this)
  61.         val builder= notification.getNotification(text)
  62.         notification.manager.notify(1, builder.build())
  63.     }
  64.  
  65.     private fun sendDefaultNotification(body: String) {
  66.  
  67.         val uri : Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
  68.  
  69.         val builder = NotificationCompat.Builder(this)
  70.             .setSmallIcon(R.drawable.ic_app_icon)
  71.             .setContentText(body)
  72.             .setSound(uri)
  73.             .setAutoCancel(true)
  74.             .setContentTitle(getString(com.shop_app.shopapp.R.string.app_name))
  75.  
  76.         val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  77.         notificationManager.notify(1, builder.build())
  78.     }
  79.  
  80.  
  81.     private fun getNotificationText(notificationName: String?):String?{
  82.         return when(notificationName){
  83.             "NotifyBasketExpireSoon" -> getString(com.shop_app.shopapp.R.string.basket_expires_soon)
  84.             "NotifyBasketArchived" -> getString(com.shop_app.shopapp.R.string.basket_archived)
  85.             "NotifyBasketPaid" -> getString(com.shop_app.shopapp.R.string.basket_paid)
  86.             "NotifyBasketCompleteEstimated" -> getString(com.shop_app.shopapp.R.string.basket_is_ready)
  87.             "NotifyBasketCompleted" -> getString(com.shop_app.shopapp.R.string.basket_completed)
  88.             "NotifyBasketServed" -> getString(com.shop_app.shopapp.R.string.basket_served)
  89.             "NotifyBasketLeaved" -> getString(com.shop_app.shopapp.R.string.basket_leaved)
  90.             else -> null
  91.         }
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement