Esempio n. 1
0
 /**
  * 建立和刷新通知
  *
  * @param progress 进度
  */
 private void showNotification(int progress) {
   if (mBuilder == null) {
     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     mBuilder = new NotificationCompat.Builder(this);
     mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
     mBuilder.setSmallIcon(R.mipmap.ic_launcher);
     mBuilder.setTicker("正在下载...");
     mBuilder.setContentTitle("正在下载...");
     mBuilder.setWhen(System.currentTimeMillis());
   }
   mBuilder.setProgress(MAX_PROGRESS, progress, false);
   mBuilder.setContentText(progress + "%");
   Notification notification = mBuilder.build();
   mNotificationManager.notify(NOTIFICATION_ID, notification);
 }
  public void launchNotification(Context context, String title, String message) {

    // Extender for Android Wear
    NotificationCompat.WearableExtender wearableExtender =
        new NotificationCompat.WearableExtender()
            .setBackground(
                BitmapFactory.decodeResource(context.getResources(), R.drawable.library_wear_bg));

    // Build notification
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
    notificationBuilder.setSmallIcon(R.drawable.ic_stat_name);
    notificationBuilder.setLargeIcon(
        BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(message);
    notificationBuilder.setLights(ContextCompat.getColor(context, R.color.pureRed), 2000, 2000);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.extend(wearableExtender);

    // TODO: criar extra no intent pra tratar abertura da pag da biblio!
    Intent resultIntent = new Intent(context, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    notificationBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // mId allows you to update the notification later on.
    int idNotify = 1;
    mNotificationManager.notify(idNotify, notificationBuilder.build());
  }
 @Override
 public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
   // Set bitmap on the notification builder, this will be picked up
   // when updateProgress is called the next time
   notificationBuilder.setLargeIcon(bitmap);
 }