Android O - Notification Channels and NotificationCompat

前端 未结 6 1367
忘了有多久
忘了有多久 2021-02-05 03:57

I cannot change this feeling: again, the Android developers came up with something new and leave everybody in the dark about how they would think the feature is used.

I

6条回答
  •  难免孤独
    2021-02-05 04:10

    If you want to support previous versions of Android (< Oreo). We may wrap the NotificationManager to create and build Notification.Builder instance in a NotificationHelper like this:

    /**
     * Helper class to manage notification channels, and create notifications.
     * 

    * Created by teocci. * * @author teocci@yandex.com on 2018-Oct-02 */ public class NotificationHelper extends ContextWrapper { public static final String NOTIFICATION_CHANNEL_PRIMARY = "notification_channel_primary"; public static final int NOTIFICATION_ID_PRIMARY = 1100; private NotificationManager manager; /** * Registers notification channels, which can be used later by individual notifications. * * @param ctx The application context */ public NotificationHelper(Context ctx) { super(ctx); // For API 26+ create notification channels if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_PRIMARY, getString(R.string.channel_name), NotificationManager.IMPORTANCE_DEFAULT ); channel.setLightColor(Color.BLUE); channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); channel.setDescription(getString(R.string.channel_description)); getManager().createNotificationChannel(channel); } } /** * Cancel a previously shown notification. If it's transient, the view * will be hidden. If it's persistent, it will be removed from the status * bar. * * @param id The ID of the notification */ public void remove(int id){ manager.cancel(id); } /** * Get a notification of type 1 *

    * Provide the builder rather than the notification it's self as useful for making notification * changes. * * @return the builder as it keeps a reference to the notification (since API 24) */ public Notification getNotification() { return getNotification(getTitle(), getBody()).build(); } /** * Get a notification of type 1 *

    * Provide the builder rather than the notification it's self as useful for making notification * changes. * * @param title the title of the notification * @param body the body text for the notification * @return the builder as it keeps a reference to the notification (since API 24) */ public Notification.Builder getNotification(String title, String body) { Notification.Builder builder = new Notification.Builder(getApplicationContext()) .setOngoing(true) // Persistent notification! .setAutoCancel(true) .setTicker(title) .setContentTitle(title) .setContentText(body) .setSmallIcon(getSmallIcon()); // Set the Channel ID for Android O. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(NOTIFICATION_CHANNEL_PRIMARY); // Channel ID } return builder; } /** * Send a notification. * * @param id The ID of the notification * @param notification The notification object */ public void notify(int id, Notification.Builder notification) { getManager().notify(id, notification.build()); } /** * Get the notification manager. *

    * Utility method as this helper works with it a lot. * * @return The system service NotificationManager */ private NotificationManager getManager() { if (manager == null) { manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } return manager; } /** * Get the small icon for this app * * @return The small icon resource id */ private int getSmallIcon() { return R.drawable.ic_smart_audio_noti_icon; } /** * Get the notification title for this app * * @return The notification title as string */ private String getTitle() { return getString(R.string.notification_title); } /** * Get the notification content for this app * * @return The notification content as string */ private String getBody() { return getString(R.string.notification_content); } }

    Then we can use it easily like this:

    @Override
    public void onCreate()
    {
        ...
        notificationHelper = new NotificationHelper(this);
        notificationHelper.notify(NotificationHelper.NOTIFICATION_ID_PRIMARY, "App is running");
        ...
    }
    
    @Override
    public void onDestroy()
    {
        notificationHelper.remove(NotificationHelper.NOTIFICATION_ID_PRIMARY)
    }
    

提交回复
热议问题