简体   繁体   中英

Oreo - how to customize the foreground service status bar

I start a foreground service that shows up in the status bar under android systems battery information.

Is there a way to customize the information (title, subtext, icon, ...) presented?

在此处输入图片说明

service code: CODE EDITED

@Override
    public void onCreate() {
        context = this.getApplicationContext();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent notificationIntent = new Intent(context, CallbackTestWidgetService.class);
            PendingIntent pendingIntent =
                    PendingIntent.getActivity(context, 10, notificationIntent, 0);

            Notification notification = new Notification.Builder(context, "Test")
                    .setContentTitle("Test")
                    .setContentText("text")
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.test)
                    .setTicker("test")
                    .build();

CharSequence name = "test";
            String description = "test";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("10", name, importance);
            channel.setDescription("test");
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);

            startForeground(10, notification);
        }

    }

What you're seeing is the default notification when no notification is posted by the app.

The reason why no notification is being posted (despite you calling startForeground ) is that you are targeting API 26 or higher and not associating your notification with a notification channel . This causes your notification to be dropped entirely as per the note on that page:

Caution: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.

You must create a notification channel, then include the id of the notification channel when building your notification.

You're building a Notification to pass to startForeground. Use the createContentView function, just like you would for a normal notification you wanted to customize.

Note that this will only work on newer versions of Android, you probably want to use the NotiicationCompat class from the support library to support version specific api differences and test on a few different APIs of emulators.

https://stackoverflow.com/users/4080424/narb https://stackoverflow.com/users/1676363/ianhanniballake

I can't change the notification content title and text by calling setContentTitle(CharSequence) and setContentText(CharSequence) . Do you know why?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM