android通知栏消息

android 在通知栏弹出消息, 看了很多博客,方到代码里都略有问题,最后只能去官方文档找示例。

最终效果
最终效果
代码中channelId 设置的部分要求 android 版本 >= 8.0
官方文档

这个通知栏既可以通过service触发,也可以通过activity触发。

public class Notify {

    private NotificationManager manager;
    private Notification.Builder builder;
    private Context context;
    private Notification notification;
    private String TAG = "notify";

    public Notify(Context context){
        this.context = context;
    }

   @TargetApi(26)
    public void setNotification(String title, String desc){

        manager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

        // set channel  chanenel的创建
        String CHANNEL_ONE_ID = "com.zhangshiling.app";
        String CHANNEL_ONE_NAME = "Channel One";
        NotificationChannel notificationChannel;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            /*notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);*/
            NotificationManager nm = context.getSystemService(NotificationManager.class);
            nm.createNotificationChannel(notificationChannel);
        }

        Intent intent = new Intent(context, message_list.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        builder = new Notification.Builder(context).setContentTitle(title)
                .setChannelId(CHANNEL_ONE_ID)
                .setContentText(desc)
                .setContentIntent(pendingIntent)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setVibrate(new long[]{0, 1000, 1000, 1000}) //通知栏消息震动
                .setLights(Color.GREEN, 1000, 2000) //通知栏消息闪灯(亮一秒间隔两秒再亮)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setSmallIcon(R.drawable.small_icon);
        Log.i(TAG,"after build a builder");
        //manager.notify(1, builder.getNotification());
        NotificationManagerCompat new_nm = NotificationManagerCompat.from(context);
        new_nm.notify(1, builder.build());  // 第一个参数1具体实现时 需要修改 用于显示不同消息。
        //return builder.getNotification();
    }
  }

在service中

 Notify nm = new Notify(this);
 nm.setNotification(rsp.title, rsp.content);

可以看下通知的效果
从上到下 从左到右
分别是 smallIcon, app_name, contentTitle, contentText
android 通知

setWhen 可以设置通知触发的时间

如果把时间设置为 +5min 展示效果 ( in 4m)

在这里插入图片描述

时间设置为 -5min 效果

在这里插入图片描述

再设置久一点 来自 19 年前的通知
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android通知是用来显示推送消息的重要组件之一。要在Android应用中实现通知推送消息,你需要进行以下步骤: 1. 创建通知渠道:从Android 8.0(API级别26)开始,你需要创建通知渠道来管理和组织通知。使用NotificationChannel类来创建通知渠道,并设置其名称、描述和重要性级别等参数。 2. 构建通知内容:使用NotificationCompat.Builder类来构建通知的内容,包括标题、文本、图标、大图等。 3. 设置点击行为:可以为通知设置点击行为,比如打开应用的某个界面或执行特定的操作。使用PendingIntent类来定义点击通知时要执行的动作。 4. 发送通知:通过NotificationManager类的notify()方法发送通知。指定一个唯一的通知ID以及之前创建的NotificationCompat.Builder对象。 下面是一个示例代码,演示了如何发送一个简单的通知: ```java // 创建通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel Description"); // 在NotificationManager中创建通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 构建通知内容 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Notification Title") .setContentText("Notification Text") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置点击行为 .setAutoCancel(true); // 点击后自动取消通知 // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值