android 状态栏通知消息

Android如何通知消息

onCreate()方法里添加
// 状态栏用
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     String channelId = "chat";
     String channelName = "通知消息";
     int importance = NotificationManager.IMPORTANCE_HIGH;
      createNotificationChannel(channelId, channelName, importance);
  }
  //调用要通知的消息
  showNotification();
    private void showNotification() {
        Intent intentMain = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentMain, 0);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "chat")
                .setContentTitle("运行中")
                .setContentText("运行中...")//内容
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(false)
                .build();
        manager.notify(1, notification);
    }
 //消息通道,状态栏用
  @TargetApi(Build.VERSION_CODES.O)
 private void createNotificationChannel(String channelId, String channelName, int importance) {
 NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
 }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值