arashforus
arashforus

Reputation: 25

How to set notification receiver in android foreground service without notification of service running ...like Whatsapp and Telegram?

I am developing a chat messenger like whatsapp and telegram and i want a foreground receiver for messages to show in notification bar ... but in the latest version of Android we should set a notification to tell that our service is running in the foreground , but in famous messengers we doesn't see this notification ... I want to know how can i do this

This is my Message_Receiver class that extends service class

class Message_Receiver : Service() {

private var mSocket: Socket? = null
lateinit var username : String
lateinit var notifmanag : NotificationManagerCompat
lateinit var notifbuilder : NotificationCompat.Builder
val CHANNEL_ID = "Messages"

lateinit var respondData : JSONObject
lateinit var type : String
lateinit var roomname : String
lateinit var roompic : String
lateinit var sender : String
lateinit var senderpic : String
lateinit var msg : String
lateinit var timeStamp : String


override fun onBind(intent: Intent?): IBinder? {
    return null
}

override fun onStartCommand(intent: Intent?, flags2: Int, startId: Int): Int {
    //Service().startForeground(11,notifbuilder.build())

    return super.onStartCommand(intent, flags2, startId)
}

override fun onDestroy() {
    super.onDestroy()
    mSocket?.disconnect()
}

override fun onCreate() {
    super.onCreate()

    username = MySharedPreference("profile",this).load("username","string") as String

    // Notification preconfigurations //////////////////////////////////////////////////////////
    notifmanag = NotificationManagerCompat.from(this)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(CHANNEL_ID, "Message_Channel", NotificationManager.IMPORTANCE_DEFAULT)
        channel.description = "Message_Channel"
        //notifmanag = getSystemService(Context.NOTIFICATION_SERVICE)
        notifmanag.createNotificationChannel(channel)
    }
    val intent2 = Intent(this, RoomActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent2, 0)

    // Conncect to socket //////////////////////////////////////////////////////////////////////   

    // Listener for new messages ///////////////////////////////////////////////////////////////
    val onNewMessage = Emitter.Listener {
        respondData = it[0] as JSONObject
        
        sender = respondData.getString("sender")
        
        notifbuilder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true)
        if (roomname == "PRIVATE"){
            notifbuilder.setContentTitle(username)
                        .setContentIntent(pendingIntent)
        }else{
            notifbuilder.setContentTitle(roomname)
                        .setContentIntent(pendingIntent)
        }
        when (type.toUpperCase(Locale.ROOT)){
            "TEXT" -> notifbuilder.setContentText(msg)
                .setSmallIcon(R.drawable.nearoom_icon2)


            "PHOTO" -> notifbuilder.setContentText("Photo : " + msg)
                .setSmallIcon(R.drawable.nearoom_icon2)


            "VIDEO" -> notifbuilder.setContentText("Video : " + msg)
                .setSmallIcon(R.drawable.nearoom_icon2)
        }

        notifmanag.notify(12,notifbuilder.build())

    }

    mSocket?.on("new message",onNewMessage)

}

}

Upvotes: 0

Views: 319

Answers (1)

emandt
emandt

Reputation: 2706

Those Apps doesn't use Services, so they don't need any Persistent Notification in the Statusbar. Push Notifications (when a message arrives) uses Firebase and the Backup at 02:00am uses JobScheduler set to that time.

Upvotes: 1

Related Questions