0

A beginner to android and have been struggling for a while on this problem now. There is an on going notification that I would like to update frequently. Possibly every second. I know this should be possible because I have seen other apps do it like Toggle. But whenever it's updated and there is some other notification on the page, they will keep fighting each other to be on the top. I have researched but haven't found anything that was able to help me on this.

I believe I have followed these rules in hoping of avoiding this issue:

  1. Use the same notification object, and not a new one for every update
  2. Use the same Id on each notify()
  3. Use the .setwhen which was recommended by another post

Video to the problem

private NotificationManager mNotificationManagerupdate;
private NotificationCompat.Builder mBuilderupdate;

/**
 * Notification system that is used for this app. All we need to do is call this function
 * when we need to trigger a notification.
 */
private void notification() {
    times = System.currentTimeMillis();

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.iss_2011);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setAutoCancel(false)
                    .setOnlyAlertOnce(true)
                    .setOngoing(true)
                    .setContentTitle("ISS Tracker")
                    .setContentText("ISS is about an hour away!")
                    .setSmallIcon(R.drawable.iss_2011)
                    .setLargeIcon(icon)
                    .setSound(soundUri)
                    .setWhen(times);

    Intent resultIntent = new Intent(context, Locations.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    stackBuilder.addParentStack(Locations.class);

    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(1234, mBuilder.build());
    mBuilderupdate = mBuilder;
    mNotificationManagerupdate = mNotificationManager;
}

/**
 * Continuously updates a notification that will display live results.
 *
 * @param time The difference between ISS' location to user's location
 */
private void updatemanager(final long time) {
    timerupdate = new Timer();
    timerupdate.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            notificationupdate(time);
        }
    }, 0, 1000);
}

/**
 * Updates the manager and finishes off when time reaches zero.
 *
 * @param time The difference between ISS' location to user's location
 */
private void notificationupdate(long time) {
    int finalseconds = (int) (time / 1000) - loop++;

    if (finalseconds > 0) {
        mBuilderupdate.setContentText("ISS is about " + finalseconds + " seconds away!");
    } else if (finalseconds > -11) {
        mBuilderupdate.setContentText("ISS is at your location! Ending in (" + (10 - Math.abs(finalseconds)) + ")");
    } else {
        timerupdate.cancel();
        timerupdate.purge();
        timerupdate = null;
        loop = 0;
        endnotification = true;
    }
    mBuilderupdate.setWhen(times);
    Log.d(TAG, "When = " + times);
    mNotificationManagerupdate.notify(1234, mBuilderupdate.build());
    if (endnotification) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) context.getSystemService(ns);
        nMgr.cancel(1234);
    }
}
Kia
  • 124
  • 1
  • 1
  • 10
  • Check out [this answer](http://stackoverflow.com/a/8743772/3572108), looks like this should solve your problem. – Bona Fide Apr 07 '16 at 10:49
  • @BonaFide Unfortunately this method did not work for me, I had it implemented in my original code. "times = System.currentTimeMillis();" and "mBuilderupdate.setWhen(times);" – Kia Apr 07 '16 at 16:21
  • @Kia - Are you able to solve this issue ? I am also facing the same issue.. – Bunny May 13 '16 at 13:36
  • I ended up updating just less frequently. Every 15 minutes. I wasn't able to fix it. – Kia May 17 '16 at 01:50

0 Answers0