如何在 android 前台服务中设置通知接收器而不通知服务 运行 ...例如 Whatsapp 和 Telegram?

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

我正在开发一个像 whatsapp 和 telegram 这样的聊天信使,我想要一个消息的前台接收器来显示在通知栏中......但是在最新版本的 Android 中,我们应该设置一个通知来告诉我们的服务在前台 运行,但在著名的 Messenger 中我们看不到此通知...我想知道我该怎么做

这是我的 Message_Receiver class 扩展服务 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)

}

}

这些应用程序不使用服务,因此它们不需要状态栏中的任何持久通知。推送通知(当消息到达时)使用 Firebase,02:00am 处的备份使用设置为该时间的 JobScheduler。