Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyFirebaseMessagingService() : FirebaseMessagingService(){
- @SuppressLint("LongLogTag")
- override fun onMessageReceived(remoteMessage: RemoteMessage) {
- Log.e("MyFirebaseMessagingService", "onMessageReceived")
- val currentToken = getSharedPreferences("prefs", Context.MODE_PRIVATE).getString(
- "current_token",
- null
- )
- if(remoteMessage.data.isNotEmpty() && currentToken == remoteMessage.to){
- val notificationName = remoteMessage.data["notificationName"]
- sendNotification(notificationName)
- }
- }
- @SuppressLint("LongLogTag")
- override fun onDeletedMessages() {
- Log.e("MyFirebaseMessagingService", "onDeletedMessages")
- super.onDeletedMessages()
- }
- @SuppressLint("LongLogTag")
- override fun onNewToken(token: String) {
- println("yeah")
- Log.e("MyFirebaseMessagingService", "onNewToken")
- sendToken(token)
- getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().putString("current_token", token).apply()
- }
- @SuppressLint("LongLogTag")
- private fun sendToken(token: String){
- Log.e("MyFirebaseMessagingService", "sendToken")
- var urlConnection: HttpURLConnection? = null
- try {
- val url = URL("https://${BASE_URL}/Account/push/$token")
- urlConnection = url.openConnection() as HttpURLConnection
- } catch (e: Exception) {
- e.printStackTrace()
- } finally {
- urlConnection?.disconnect()
- }
- }
- @SuppressLint("LongLogTag")
- fun sendNotification(notificationName: String?) {
- Log.e("MyFirebaseMessagingService", "sendNotification")
- val text = getNotificationText(notificationName)
- if(text!=null){
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- sendOreoNotification(text)
- } else {
- sendDefaultNotification(text)
- }
- }
- }
- @RequiresApi(api = Build.VERSION_CODES.O)
- private fun sendOreoNotification(text: String) {
- val notification = OreoNotification(this)
- val builder= notification.getNotification(text)
- notification.manager.notify(1, builder.build())
- }
- private fun sendDefaultNotification(body: String) {
- val uri : Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
- val builder = NotificationCompat.Builder(this)
- .setSmallIcon(R.drawable.ic_app_icon)
- .setContentText(body)
- .setSound(uri)
- .setAutoCancel(true)
- .setContentTitle(getString(com.shop_app.shopapp.R.string.app_name))
- val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- notificationManager.notify(1, builder.build())
- }
- private fun getNotificationText(notificationName: String?):String?{
- return when(notificationName){
- "NotifyBasketExpireSoon" -> getString(com.shop_app.shopapp.R.string.basket_expires_soon)
- "NotifyBasketArchived" -> getString(com.shop_app.shopapp.R.string.basket_archived)
- "NotifyBasketPaid" -> getString(com.shop_app.shopapp.R.string.basket_paid)
- "NotifyBasketCompleteEstimated" -> getString(com.shop_app.shopapp.R.string.basket_is_ready)
- "NotifyBasketCompleted" -> getString(com.shop_app.shopapp.R.string.basket_completed)
- "NotifyBasketServed" -> getString(com.shop_app.shopapp.R.string.basket_served)
- "NotifyBasketLeaved" -> getString(com.shop_app.shopapp.R.string.basket_leaved)
- else -> null
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement