NotificationService.kt 3.96 KB
Newer Older
Martinus123S committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
package ta1.cis.service

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.pusher.client.Pusher
import com.pusher.client.PusherOptions
import com.pusher.client.connection.ConnectionEventListener
import com.pusher.client.connection.ConnectionState
import com.pusher.client.connection.ConnectionStateChange
import ta1.cis.R
import ta1.cis.activity.HomeActivity
import ta1.cis.helper.SharedPref

class NotificationService : Service() {

    private lateinit var pusher : Pusher;
    private lateinit var notificationManager: NotificationManager
    private lateinit var sharedPreferences: SharedPref

    override fun onCreate() {
        super.onCreate()
        pusher = setUpPusher()
        sharedPreferences = SharedPref(this)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        super.onStartCommand(intent, flags, startId)
        val channel = pusher.subscribe("cis_channel")
        channel.bind("event-user-${sharedPreferences.getUserId()}") { event ->
            Log.d("Pusher1",event.data.toString())
            createNotification()
        }
        return START_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? {
        TODO("Not yet implemented")
        return null
    }

    private fun createNotification() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = getString(R.string.channel_name)
            val descriptionText = getString(R.string.channel_description)
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel("tes", name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            notificationManager= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
        var builder = NotificationCompat.Builder(this, "tes")
            .setSmallIcon(R.drawable.logo_foreground  )
            .setContentTitle("Izin Bermalam")
            .setContentText("Izin bermalam sudah di setujui")
            .setStyle(NotificationCompat.BigTextStyle()
                .bigText("Izin bermalam sudah di setujui"))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        val intent = Intent(this, HomeActivity::class.java)
        intent.putExtra("NotifType", "IB")
        val pendingIntent = PendingIntent
            .getActivity(this, 0, intent
                ,PendingIntent.FLAG_UPDATE_CURRENT)

        builder.setContentIntent(pendingIntent)
        builder.setAutoCancel(true)
        val mNotificationManager =
            this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager


        mNotificationManager.notify(1,builder.build())
    }

    fun setUpPusher() : Pusher{
        val options = PusherOptions()
        options.setCluster("ap1");

        val pusher = Pusher("cf80100d02b84bc296bd", options)

        pusher.connect(object : ConnectionEventListener {
            override fun onConnectionStateChange(change: ConnectionStateChange) {
                Log.i("Pusher", "State changed from ${change.previousState} to ${change.currentState}")
            }

            override fun onError(
                message: String,
                code: String,
                e: Exception
            ) {
                Log.i("Pusher", "There was a problem connecting! code ($code), message ($message), exception($e)")
            }
        }, ConnectionState.ALL)
        return pusher;
    }
}