示例#1
0
 public static void createBigNotification(
     Context context,
     Intent intent,
     String contentTitle,
     String contentText,
     List<String> lista,
     int id) {
   //        PendingIntent pendingIntent = getPendingIntent(context, intent, id);
   PendingIntent pendingIntent =
       PendingIntent.getActivity(
           context,
           0,
           new Intent(context, TodoListActivity.class),
           PendingIntent.FLAG_UPDATE_CURRENT);
   int size = lista.size();
   NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
   inboxStyle.setBigContentTitle(contentTitle);
   for (String s : lista) {
     inboxStyle.addLine(s);
   }
   inboxStyle.setSummaryText(contentText);
   NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
   builder.setDefaults(Notification.DEFAULT_ALL);
   builder.setSmallIcon(R.mipmap.ic_launcher);
   builder.setContentTitle(contentTitle);
   builder.setContentText(contentText);
   builder.setContentIntent(pendingIntent);
   builder.setAutoCancel(true);
   builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
   builder.setNumber(size);
   builder.setStyle(inboxStyle);
   NotificationManagerCompat nm = NotificationManagerCompat.from(context);
   nm.notify(id, builder.build());
 }
  /**
   * Build a new notification. To update the progress on the notification, use {@link
   * #updateProgress(int, int)} instead.
   *
   * @param episode The episode playing.
   * @param paused Playback state, <code>true</code> for paused.
   * @param canSeek If the currently played media is seekable.
   * @param position The current playback progress.
   * @param duration The length of the current episode.
   * @param session The media session representing current playback.
   * @return The notification to display.
   */
  @NonNull
  public Notification build(
      Episode episode,
      boolean paused,
      boolean canSeek,
      int position,
      int duration,
      MediaSessionCompat session) {
    // 0. Prepare the main intent (leading back to the app)
    appIntent.putExtra(PODCAST_URL_KEY, episode.getPodcast().getUrl());
    appIntent.putExtra(EPISODE_URL_KEY, episode.getMediaUrl());
    final PendingIntent backToAppIntent =
        PendingIntent.getActivity(context, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // 1. Create the notification builder and set values
    notificationBuilder = new NotificationCompat.Builder(context);
    notificationBuilder
        .setContentIntent(backToAppIntent)
        .setTicker(episode.getName())
        .setSmallIcon(R.drawable.ic_stat)
        .setContentTitle(episode.getName())
        .setContentText(episode.getPodcast().getName())
        .setWhen(0)
        .setProgress(duration, position, false)
        .setOngoing(true);

    // 2. Load large image if available, see onBitmapLoaded() below
    if (episode.getPodcast().hasLogoUrl())
      Picasso.with(context)
          .load(episode.getPodcast().getLogoUrl())
          .resizeDimen(
              android.R.dimen.notification_large_icon_width,
              android.R.dimen.notification_large_icon_height)
          .into(this);

    // 3. Add actions to notification
    notificationBuilder.addAction(stopAction);
    if (canSeek) notificationBuilder.addAction(rewindAction);
    if (paused) notificationBuilder.addAction(playAction);
    else notificationBuilder.addAction(pauseAction);
    if (canSeek) notificationBuilder.addAction(forwardAction);

    // 4. Apply other notification features
    NotificationCompat.MediaStyle style =
        new NotificationCompat.MediaStyle().setMediaSession(session.getSessionToken());
    // Make sure not to show rew/ff icons for live streams
    if (canSeek) style.setShowActionsInCompactView(1, 2, 3); // rewind, toggle play, forward
    else style.setShowActionsInCompactView(0, 1); // stop, toggle play

    notificationBuilder.setStyle(style);
    notificationBuilder.setColor(ContextCompat.getColor(context, R.color.theme_dark));
    notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    notificationBuilder.setCategory(NotificationCompat.CATEGORY_TRANSPORT);

    return notificationBuilder.build();
  }