@SuppressWarnings("deprecation")
  @SuppressLint("NewApi")
  public static void showNotification(Intent intent, Context c) {
    if (context == null) {
      context = c;
    }
    NotificationManager nm =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent _intent = new Intent();

    /*if (hasAction) {
    	if (action.equalsIgnoreCase("View")) {
    		_intent = context.getPackageManager()
    				.getLaunchIntentForPackage(context.getPackageName());
    	}
    }*/
    _intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());

    int _icon = icon;
    String _message = message;
    int _badge = badge;
    long _time = time;
    String _action = action;

    if (intent != null) {
      _icon = intent.getIntExtra("icon", 0);
      _message = intent.getStringExtra("message");
      _badge = intent.getIntExtra("badge", 0);
      _time = intent.getLongExtra("time", new Date().getTime());
      _action = intent.getStringExtra("action");
    }

    Notification n = null;
    PendingIntent pi = PendingIntent.getActivity(context, 0, _intent, 0);
    if (Build.VERSION.SDK_INT >= 11) {
      Notification.Builder builder = new Notification.Builder(context);
      builder
          .setContentIntent(pi)
          .setSmallIcon(_icon)
          .setTicker(_message)
          .setNumber(_badge)
          .setWhen(_time)
          .setAutoCancel(true)
          .setContentTitle(_action)
          .setContentText(_message);
      n = builder.getNotification();
    } else {
      n = new Notification();
      n.contentIntent = pi;
      n.icon = android.R.drawable.ic_dialog_info;
      n.tickerText = _message;
      n.number = _badge;
      n.when = _time;
      n.flags |= Notification.FLAG_AUTO_CANCEL;
      n.setLatestEventInfo(context, _action, _message, pi);
    }

    nm.notify((int) (_time / 1000), n);
    showStatusBar();
  }
Exemplo n.º 2
0
  public void notifyMe(View v) {
    Notification note =
        new Notification(
            R.drawable.stat_notify_chat, "Status message!", System.currentTimeMillis());
    PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, NotifyMessage.class), 0);

    note.setLatestEventInfo(this, "Notification Title", "This is the notification message", i);
    note.number = ++count;
    note.vibrate = new long[] {500L, 200L, 200L, 500L};
    note.flags |= Notification.FLAG_AUTO_CANCEL;

    mgr.notify(NOTIFY_ME_ID, note);
  }
  /**
   * Handles the data returned by UnreadPmCount
   *
   * @param obj
   * @throws Exception
   */
  public void handleUnreadPMData(JSONObject obj) throws Exception {
    int messageCount = obj.getInt("unreadpmcount");

    if (messageCount > 0) {
      NotificationManager manager;
      Notification note;
      PendingIntent pendingIntent;
      Intent intent;

      // Get the notification manager system service
      manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

      // Create a new notification
      note = new Notification(R.drawable.icon, "New SoFurry PM(s)", System.currentTimeMillis());

      // Create the Intent and wrap it in a PendingIntent
      //            intent = new Intent(this, ListPMActivity.class);
      intent = new Intent(this, SFBrowsePMActivity.class);

      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

      String message = "You have " + messageCount + " new unread message";
      if (messageCount > 1) message += "s";

      message += ".";
      // Set some settings for the notification
      note.setLatestEventInfo(this, "SoFurry PM", message, pendingIntent);

      note.vibrate = AppConstants.VIBRATE_PM_INCOMING;
      note.ledARGB = 0x0000FFFF; // Cyan
      note.ledOffMS = 1500;
      note.ledOnMS = 500;
      note.number = messageCount;
      note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

      // Fire off the notification
      manager.notify(AppConstants.NOTIFICATION_ID_PM, note);
    }

    // Set timestamp of when we last checked, for incremental message checks
    PmNotificationService.lastCheck_ = (System.currentTimeMillis() / 1000);
    prefs
        .edit()
        .putLong(AppConstants.PREFERENCE_LAST_PM_CHECK_TIME, PmNotificationService.lastCheck_)
        .commit();
  }
Exemplo n.º 4
0
  @SuppressWarnings("deprecation")
  public static void notificate(Activity activity, String title, String message) {

    int count = 0;
    NotificationManager mgr =
        (NotificationManager) activity.getSystemService(Activity.NOTIFICATION_SERVICE);
    int NOTIFY_ME_ID = 1987;

    Notification note =
        new Notification(
            R.drawable.train_icon, "TrainCompany message!", System.currentTimeMillis());

    Intent messageIntent = new Intent(activity, NotificationMessage.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, messageIntent, 0);

    note.setLatestEventInfo(activity, title, message, pendingIntent);
    note.number = ++count;
    note.vibrate = new long[] {500L, 200L, 200L, 500L};
    note.flags |= Notification.FLAG_AUTO_CANCEL;
    mgr.notify(NOTIFY_ME_ID, note);
  }
Exemplo n.º 5
0
  public void setNotificationManager() {
    String ns = Context.NOTIFICATION_SERVICE;
    mNM = (NotificationManager) mContext.getSystemService(ns);

    // Instantiate the Notification:
    long when = System.currentTimeMillis();
    Notification n = new Notification(mIcon, mTickerText, when);
    n.flags |=
        Notification.FLAG_AUTO_CANCEL; // when the user selects the notification, it clears itself
    n.number = Recorder.getTimesliceCount();

    // Define the Notification's expanded message and Intent:
    Intent notificationIntent = new Intent(mContext, Recorder.class);
    PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
    n.setLatestEventInfo(mContext, mContentTitle, mContentText, contentIntent);
    // TODO: blinkenlights, etc. (currently crashes on next line)
    // notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE |
    // Notification.DEFAULT_LIGHTS;

    // Pass the Notification to the NotificationManager:
    mNM.notify(NOTIFY_ID, n);
  }
  @TestTargets({
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "writeToParcel",
        args = {android.os.Parcel.class, int.class}),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "Notification",
        args = {android.os.Parcel.class})
  })
  public void testWriteToParcel() {
    mNotification = new Notification();
    mNotification.icon = 0;
    mNotification.number = 1;
    final Intent intent = new Intent();
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
    mNotification.contentIntent = pendingIntent;
    final Intent deleteIntent = new Intent();
    final PendingIntent delPendingIntent = PendingIntent.getBroadcast(mContext, 0, deleteIntent, 0);
    mNotification.deleteIntent = delPendingIntent;
    mNotification.tickerText = TICKER_TEXT;

    final RemoteViews contentView =
        new RemoteViews(
            mContext.getPackageName(),
            com.android.internal.R.layout.status_bar_latest_event_content);
    mNotification.contentView = contentView;
    mNotification.defaults = 0;
    mNotification.flags = 0;
    final Uri uri = Uri.parse(URI_STRING);
    mNotification.sound = uri;
    mNotification.audioStreamType = 0;
    final long[] longArray = {1l, 2l, 3l};
    mNotification.vibrate = longArray;
    mNotification.ledARGB = 0;
    mNotification.ledOnMS = 0;
    mNotification.ledOffMS = 0;
    mNotification.iconLevel = 0;
    Parcel parcel = Parcel.obtain();
    mNotification.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    // Test Notification(Parcel)
    Notification result = new Notification(parcel);
    assertEquals(mNotification.icon, result.icon);
    assertEquals(mNotification.when, result.when);
    assertEquals(mNotification.number, result.number);
    assertNotNull(result.contentIntent);
    assertNotNull(result.deleteIntent);
    assertEquals(mNotification.tickerText, result.tickerText);
    assertNotNull(result.contentView);
    assertEquals(mNotification.defaults, result.defaults);
    assertEquals(mNotification.flags, result.flags);
    assertNotNull(result.sound);
    assertEquals(mNotification.audioStreamType, result.audioStreamType);
    assertEquals(mNotification.vibrate[0], result.vibrate[0]);
    assertEquals(mNotification.vibrate[1], result.vibrate[1]);
    assertEquals(mNotification.vibrate[2], result.vibrate[2]);
    assertEquals(mNotification.ledARGB, result.ledARGB);
    assertEquals(mNotification.ledOnMS, result.ledOnMS);
    assertEquals(mNotification.ledOffMS, result.ledOffMS);
    assertEquals(mNotification.iconLevel, result.iconLevel);

    mNotification.contentIntent = null;
    parcel = Parcel.obtain();
    mNotification.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    result = new Notification(parcel);
    assertNull(result.contentIntent);

    mNotification.deleteIntent = null;
    parcel = Parcel.obtain();
    mNotification.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    result = new Notification(parcel);
    assertNull(result.deleteIntent);

    mNotification.tickerText = null;
    parcel = Parcel.obtain();
    mNotification.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    result = new Notification(parcel);
    assertNull(result.tickerText);

    mNotification.contentView = null;
    parcel = Parcel.obtain();
    mNotification.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    result = new Notification(parcel);
    assertNull(result.contentView);

    mNotification.sound = null;
    parcel = Parcel.obtain();
    mNotification.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    result = new Notification(parcel);
    assertNull(result.sound);
  }
  // Checks the database then creates and pushes notifications and alerts to the android.
  public void showNotification() {
    String s;
    count = 0;

    // Load the shared preferences
    LoadPreferences();

    if (notifOn) {
      // Open the database adapter
      adapter.open();

      s = adapter.fetchAll(5);
      count = Integer.parseInt(s.substring(2));

      // BROKEN: fetch all of the items about to expire
      // l = new ArrayList <Item> (adapter.fetchAll());

      // Close the database adapter
      adapter.close();

      // If notifications are on
      if (count > 0) {
        Notification note =
            new Notification(R.drawable.icon, "Food Expiring", System.currentTimeMillis());

        PendingIntent in =
            PendingIntent.getActivity(this, 0, new Intent(this, PantryProtectorActivity.class), 0);

        note.icon = R.drawable.icon;
        note.tickerText = "Food Expiring";
        note.when = System.currentTimeMillis();
        note.number = count;
        note.flags |= Notification.FLAG_AUTO_CANCEL;

        // Add flashing to the notifications
        if (flashOn && s.charAt(1) == '1') {
          // add lights to notifications
          if (debug) Log.i("DEBUG", ">>>>> Flashing. >>>>>");
          note.flags |= Notification.FLAG_SHOW_LIGHTS;
          note.ledARGB = Color.CYAN;
          note.ledOnMS = 500;
          note.ledOffMS = 500;
        }

        // Add vibration to notifications
        if (vibrateOn && s.charAt(0) == '1') {
          // add vibration to notifications
          if (debug) Log.i("DEBUG", ">>>>> ViBrAtInG. >>>>>");
          note.vibrate = new long[] {100, 200, 200, 200, 200, 200, 1000, 200, 200, 200, 1000, 200};
        }

        note.setLatestEventInfo(
            this, "Items are about to expire!", count + " items are about to expire!", in);

        notifManager.notify(NOTIF_ID, note);
      }

      // Debug statement for checking proper data flow
      if (debug && (date.getHours() != mHour || date.getMinutes() != mMinute)) {
        System.out.println(
            "Original Schedule: "
                + date.getDay()
                + ","
                + date.getHours()
                + ":"
                + date.getMinutes());
        date = new Date();
        date.setHours(mHour);
        date.setMinutes(mMinute);
        timer.cancel();
        System.out.println(
            "Changed Schedule: " + date.getDay() + "," + date.getHours() + ":" + date.getMinutes());
        startschedule();
      }
    }
  }