为了账号安全,请及时绑定邮箱和手机立即绑定

Android开发笔记——适配Android8.0 通知

标签:
Android

1.调用generateNotification(BeanAlarm beanAlarm) 方法。

private              Context        context                                      = null;
private static       int            nId                                          = 0;
private void generateNotification(BeanAlarm beanAlarm) {
    SharedUtils.saveIsNotification(context, true);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager == null) {
        return;
    }
    String guid = beanAlarm.getAlarmGuid();

    if (guid != null) {
        if(beanAlarm.getName() == null){
            beanAlarm.setName(context.getString(R.string.name));
        }
        if(beanAlarm.getSimilarity() == null){
            beanAlarm.setSimilarity("");
        }
        if(beanAlarm.getNote() == null){
            beanAlarm.setNote(context.getString(R.string.notification_null_notes));
        }
        if(beanAlarm.getNote().isEmpty()){
            beanAlarm.setNote(context.getString(R.string.notification_empty_notes));
        }
        String subject = beanAlarm.getName() + " " + beanAlarm.getSimilarity();
        String notes = beanAlarm.getNote();

        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.putExtra("Guid", guid);
        //notificationIntent.setAction(Intent.ACTION_MAIN);
        //notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, nId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (notificationManager.getNotificationChannel(FACE_DETECTION_ALARM_NOTIFICATION_CHANNEL_ID) == null) {
                createNotificationChannel(notificationManager);
            }
            notification = getChannelNotification(subject, notes, intent).build();
        } else {
            notification = getNotification_25(subject, notes, intent).build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
        }

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(nId++, notification);
    }
}

private void createNotificationChannel(NotificationManager notificationManager) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return;
    }
    NotificationChannel channel = new NotificationChannel(FACE_DETECTION_ALARM_NOTIFICATION_CHANNEL_ID, "xxxxxx",
            NotificationManager.IMPORTANCE_HIGH);
    channel.enableLights(true);
    notificationManager.createNotificationChannel(channel);

}

private Notification.Builder getChannelNotification(String subject, String message, PendingIntent intent) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return null;
    }
    return new Notification.Builder(context, FACE_DETECTION_ALARM_NOTIFICATION_CHANNEL_ID).setLargeIcon(BitmapFactory.decodeResource(context.getResources
            (), R.mipmap.ic_launcher)).setSmallIcon(R.mipmap.icon_notification_small).setContentIntent(intent).setContentTitle(subject).setContentText(message)
            .setAutoCancel(true).setShowWhen(true).setVisibility(Notification.VISIBILITY_PUBLIC);
}

private NotificationCompat.Builder getNotification_25(String subject, String message, PendingIntent intent) {
    return new NotificationCompat.Builder(context).setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)).setSmallIcon
            (R.mipmap.icon_notification_small).setContentIntent(intent).setContentTitle(subject).setContentText(message).setAutoCancel(true).setShowWhen(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setPriority(NotificationCompat.PRIORITY_HIGH).setDefaults(Notification.DEFAULT_SOUND |
                    Notification.DEFAULT_VIBRATE);
}

原文链接:http://www.apkbus.com/blog-184446-78090.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消