Android 在API26之后NotificationCompat.Builder (Context context)被废弃,创建通知需要使用新的构造器NotificationCompat.Builder (Context context, String channelId)

so:

NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);String CHANNEL_ID = "com.example.test";//自定义的字符串String CHANNEL_NAME = "TEST";NotificationChannel notificationChannel = null;NotificationCompat.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {    notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    notificationManager.createNotificationChannel(notificationChannel);}Intent intent = new Intent(this, HomeActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder = new NotificationCompat.Builder(this, CHANNEL_ID).        setContentTitle(getResources().getString(R.string.app_name)).        setContentText(getResources().getString(R.string.notice_001)).        setWhen(System.currentTimeMillis()).        setSmallIcon(R.mipmap.app_icon).        setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon)).        setContentIntent(pendingIntent).setDefaults(NotificationCompat.FLAG_ONGOING_EVENT)        .setPriority(Notification.PRIORITY_MAX);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//SDK版本>=21才能设置悬挂式通知栏    builder.setCategory(String.valueOf(Notification.FLAG_ONGOING_EVENT))            .setVisibility(Notification.VISIBILITY_PUBLIC)            .setColor(getResources().getColor(R.color.white));    Intent intent2 = new Intent();    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent2, 0);    builder.setFullScreenIntent(pi, true);}Notification notification = builder.build();manager.notify(1, notification);

更多相关文章

  1. Android下Notification(通知栏)的使用
  2. 转:Android推送通知指南
  3. Android的通知栏点击后无反应解决办法
  4. Android Notification 通知
  5. android 通知提醒
  6. Android监听消息通知栏点击事件
  7. TextUtils类-Android字符串处理类
  8. 全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本
  9. android通知栏进度条

随机推荐

  1. android实现调用系统音乐播放器
  2. android 5.1 API简介
  3. Android下拉刷新控件
  4. Android安装或者卸载应用APK
  5. Android(安卓)Loader详解
  6. android各种触摸事件的处理,touchEvent
  7. Android(安卓)基本控件
  8. Android(安卓)分享微信小程序
  9. 节操视频播放器
  10. android 调用系统应用