Exemplo n.º 1
0
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  private void triggerNotification(String info, String info2, Context context, Intent intent) {
    NotificationCompat.Builder mBuilder =
        (NotificationCompat.Builder)
            new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_directions_bus_black_48dp)
                .setContentTitle(context.getResources().getString(R.string.app_name))
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(info2))
                .setContentText(info)
                .setVibrate(null)
                .setSound(null)
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), 0));

    // Notifications
    if (Repository.getInstance().getRingtoneNotification().equals(true)) {
      // Ton
      Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      mBuilder.setSound(alarmSound);
    }
    if (Repository.getInstance().getVibrationNotification().equals(true)) {
      // Vibration
      mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});
    }
    if (Repository.getInstance().getLedNotification().equals(true)) {
      // LED
      mBuilder.setLights(ContextCompat.getColor(context, R.color.ledColour), 1000, 1000);
    }

    // Creates an explicit intent for an Activity
    Intent resultIntent = new Intent(context, MainActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);

    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(418, mBuilder.build());
  }
Exemplo n.º 2
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);
  }