亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

狀態欄中未顯示 Android 前臺服務通知

狀態欄中未顯示 Android 前臺服務通知

catspeake 2023-03-17 10:11:02
我已經在 SO 上嘗試了各種解決方案,但似乎無法讓它們中的任何一個工作。通知根本不會顯示。誰能指出我的代碼中可能存在的問題?private void startRunningInForeground() {        setupNotificationChannel();        Intent showTaskIntent = new Intent(getApplicationContext(), MainActivity.class);        showTaskIntent.setAction(Intent.ACTION_MAIN);        showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);        showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        PendingIntent contentIntent = PendingIntent.getActivity(                getApplicationContext(),                0,                showTaskIntent,                PendingIntent.FLAG_UPDATE_CURRENT);        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, SERVICE_NOTIFICATION_CHANNEL);        builder.setSmallIcon(R.drawable.ic_stat_ic_logo);        builder.setContentTitle(getString(R.string.merge_notif_title));        builder.setContentText(getString(R.string.merge_notif_description));        builder.setContentIntent(contentIntent);        builder.setWhen(System.currentTimeMillis());        builder.setAutoCancel(false);        builder.setOngoing(true);        startForeground(NOTIFICATION_ID, builder.build());    }    private void setupNotificationChannel() {        // Only run this on versions of Android that require notification channels.        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {            return;        }        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        if(notificationManager == null) {            return;        }    }  沒有收到任何錯誤消息并且正在創建通知渠道,因為我在應用程序設置中看到了它。
查看完整描述

2 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

所以我能夠通過擴展服務而不是 IntentService 來讓它工作。我無法解釋為什么會這樣,但現在一切都按預期進行。



查看完整回答
反對 回復 2023-03-17
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

你的 startForeground 做什么?


嘗試修改它并確保您的操作系統> Android 8.0


順便說一句,頻道只需要創建一次。


PendingIntent contentIntent = PendingIntent.getActivity(

            getApplicationContext(),

            NOTIFICATION_ID, <---

            showTaskIntent,

            PendingIntent.FLAG_UPDATE_CURRENT);



startForeground(NOTIFICATION_ID, builder.build());

notificationManager.notify(NOTIFICATION_ID, builder.build()); <--

可能是因為 Android O bg 服務限制


//Start service:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

  startForegroundService(new Intent(this, YourService.class));

} else {

  startService(new Intent(this, YourService.class));

}


查看完整回答
反對 回復 2023-03-17
  • 2 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號