我開發了一個簡單的 GPS 跟蹤應用程式,它使用前臺服務來保持應用程式的運行。會有一個通知顯示我的應用程式正在運行,但是如何在徽標旁邊隱藏應用程式名稱?我只想要徽標而不顯示應用程式名稱。

uj5u.com熱心網友回復:
要使服務在前臺保持活動狀態,您可以創建自己的通知并根據需要對其進行自定義。
您可以將此示例代碼用作基礎。
lateinit var notificationBuilder: Notification
private fun setNotification(title: String, descr: String) {
val pendingIntent = PendingIntent.getActivity(
this, 0, Intent(
this,
MainActivity::class.java
), 0
)
createNotificationChannel()
notificationBuilder = NotificationCompat.Builder(this, "misc")
.setContentTitle(title)
.setContentText(descr)
.setOnlyAlertOnce(true)
.setSmallIcon(R.drawable.ic_nordic_icon)
.setContentIntent(pendingIntent)
.build()
startForeground(1, notificationBuilder)
}
lateinit var manager: NotificationManager
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
"misc",
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
manager = getSystemService(
NotificationManager::class.java
)
manager.createNotificationChannel(serviceChannel)
}
}
并在您的服務中呼叫它,例如:
setNotification(
"My title",
"A notification description"
)
官方檔案在這里。
更新
您可以使用以下配置隱藏通知:
serviceChannel = new NotificationChannel(
"misc",
"Foreground Service Channel",
NotificationManager.IMPORTANCE_UNSPECIFIED
);
notification = new NotificationCompat.Builder(this, "misc")
.setPriority(Notification.PRIORITY_MIN)
.setContentTitle(title)
.setContentText(descr)
.setContentIntent(pendingIntent)
.build();
如果對您有幫助,請記住將您的答案標記為正確。謝謝你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/408429.html
標籤:
