java #android #oreo #notification builder

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java #android #oreo #notification builder相关的知识,希望对你有一定的参考价值。

@TargetApi(26)
public static class O {

    public static final String CHANNEL_ID = 
        String.valueOf(getRandomNumber());

    public static void createNotification(Service context) {
        String channelId = createChannel(context);
        Notification notification = buildNotification(context, channelId);
        context.startForeground(ONGOING_NOTIFICATION_ID, notification);
    }

    private static Notification buildNotification(Service context, 
                                                  String channelId) {
        // Create Pending Intents.
        PendingIntent piLaunchMainActivity = getLaunchActivityPI(context);
        PendingIntent piStopService = getStopServicePI(context);

        // Action to stop the service.
        Notification.Action stopAction =
                new Notification.Action
                    .Builder(STOP_ACTION_ICON,
                             getNotificationStopActionText(context),
                             piStopService)
                    .build();

        // Create a notification.
        return new Notification.Builder(context, channelId)
                .setContentTitle(getNotificationTitle(context))
                .setContentText(getNotificationContent(context))
                .setSmallIcon(SMALL_ICON)
                .setContentIntent(piLaunchMainActivity)
                .setActions(stopAction)
                .setStyle(new Notification.BigTextStyle())
                .build();
    }

    @NonNull
    private static String createChannel(Service context) {
        // Create a channel.
        NotificationManager notificationManager = 
                (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence channelName = "Playback channel";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel notificationChannel =
                new NotificationChannel(CHANNEL_ID, 
                                        channelName, 
                                        importance);
        notificationManager
            .createNotificationChannel(notificationChannel);
        return CHANNEL_ID;
    }
}

以上是关于java #android #oreo #notification builder的主要内容,如果未能解决你的问题,请参考以下文章

java Android Noti

Android oreo - 推送通知崩溃

android Oreo Crash错误

Android Nougat,Oreo - 如何将长按动作添加到快速切换?

Android Oreo onTaskRemoved 事件不起作用(Android 版本 Oreo)

Android 8 (Oreo) 中的后台位置限制是啥?