Esempio n. 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());
 }
Esempio n. 2
0
 public static void create(
     Context context, Intent intent, String contentTitle, String contentText, int id) {
   PendingIntent pendingIntent = getPendingIntent(context, intent, id);
   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);
   NotificationManagerCompat nm = NotificationManagerCompat.from(context);
   nm.notify(id, builder.build());
 }
Esempio n. 3
0
 public static void creatHeadsUpNotification(
     Context context, Intent intent, String contentTitle, String contentText, int id) {
   PendingIntent pendingIntent = getPendingIntent(context, intent, id);
   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.setColor(Color.BLUE);
   builder.setFullScreenIntent(pendingIntent, false);
   NotificationManagerCompat nm = NotificationManagerCompat.from(context);
   nm.notify(id, builder.build());
 }
Esempio n. 4
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);
 }
Esempio n. 5
0
 public static void sendBroadcastNotification(
     Context context, Intent intent, String contentTitle, String contentText, int id) {
   NotificationManager manager =
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   PendingIntent pendingIntent =
       PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
   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.setVibrate(new long[] {200});
   builder.setAutoCancel(true);
   builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
   Notification n = builder.build();
   manager.notify(id, n);
 }
Esempio n. 6
0
  public void simpleNotification() {
    //        获取NotificationManager实例
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //        构造Notification.Builder 对象
    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);

    //        设置Notification图标
    builder.setSmallIcon(R.mipmap.ic_launcher);
    // builder.setLargeIcon(myIcon);
    //        设置Notification tickertext
    builder.setTicker("A new Message");
    //        设置通知的题目
    Date dNow = new Date();
    SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
    String time = ft.format(dNow);
    builder.setContentTitle(this.todayWeather.getCity() + "\t" + "更新时间为" + time);
    //        设置通知的内容
    builder.setContentText("今天温度为:" + this.todayWeather.getWendu() + "℃");
    builder.setContentInfo("Info");
    //        设置通知可以被自动取消
    builder.setAutoCancel(true);
    //        设置通知栏显示的Notification按时间排序
    builder.setWhen(System.currentTimeMillis());
    //        设置其他物理属性,包括通知提示音、震动、屏幕下方LED灯闪烁
    builder.setSound(
        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); // 这里设置一个本地文件为提示音
    //        builder.setVibrate(new long[]{1000,1000,1000,1000});
    builder.setLights(Color.BLUE, 0, 1);
    //        设置该通知点击后将要启动的Intent,这里需要注意PendingIntent的用法,构造方法中的四个参数(context,int
    // requestCode,Intent,int flags);
    Intent intent = new Intent(MainActivity.this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
    builder.setContentIntent(pi);

    //        实例化Notification

    Notification notification =
        builder.build(); // notify(int id,notification对象);id用来标示每个notification
    manager.notify(1, 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());
  }