android 8.0 notification

public static void sendNotificationCompat(Context ctx, int id, String title, String body) {
  sendNotificationCompat(ctx, id, title, body, null);
}

public static void sendNotificationCompat(Context ctx, int id, String title, String body, PendingIntent pendingIntent) {
  Context context = App.get();
  NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  if (nm == null) {
    return;
  }

  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = nm.getNotificationChannel(CHANNEL_ID);
    if (notificationChannel == null) {
      NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
      nm.createNotificationChannel(channel);
    }
  }

  Notification.Builder builder = new Notification.Builder(ctx)
      .setSmallIcon(R.mipmap.ic_launcher)
      .setContentText(body)
      .setAutoCancel(true)
      .setTicker(body);

  if (!TextUtils.isEmpty(title)) {
    builder.setContentTitle(title);
  }

  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    builder.setChannelId(CHANNEL_ID);
  }

  if (pendingIntent != null) {
    builder.setContentIntent(pendingIntent);
  }
  nm.notify(id, builder.build());
}

你可能感兴趣的:(android)