前台服务通知问题

时间:2021-06-04 13:26:15

标签: android notifications foreground-service

我有一个前台服务,我通过它构建通知。即使它们运行相同的操作系统,setColor 也不适用于所有设备。

有人见过这个问题吗?有什么解决方法的提示吗?

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String input = intent.getStringExtra("inputExtra");
    createNotificationChannel();

    Spannable spannable = new SpannableString(input);
    spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, input.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.addLine(spannable);

    Intent fullScreenIntent = CallStateObserver.launchCallScreenIntent(getApplicationContext(), "", null, null);
    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), fullScreenIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle(Build.VERSION.SDK_INT == 23 ? getApplicationContext().getResources().getString(R.string.app_name):"")
            .setContentText(input)
            .setGroup("false")
            .setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(android.R.drawable.ic_menu_call)
            .setFullScreenIntent(fullScreenPendingIntent, true)
            .setColorized(true)
            .setUsesChronometer(true)
            .setStyle(inboxStyle)
            .setColor(ContextCompat.getColor(getApplicationContext(), R.color.accent))
            .build();
    startForeground(1, notification);
    return START_STICKY;
}

0 个答案:

没有答案