Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add notification timer like whatsapp call notification?

I have a notification builder. I want to add notification timer like whatsapp call notification. So if I call updateNotifTime function, is not working.

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, callChannelId)
                .setSmallIcon(R.drawable.call_icon)
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .setShowWhen(true)
                .setWhen(System.currentTimeMillis())
                .setPriority(NotificationCompat.PRIORITY_HIGH);

public void updateNotifTime() {
    this.callingElapsedRunnable = new Runnable() {
                @Override
                public void run() {

                    elapsedTime += 1000;

                    String timer = DateUtils.calculateTime(elapsedTime);

                    builder.setWhen(elapsedTime);

                    callingElapsedHandler.postDelayed(this, 1000);
                }
            };

callingElapsedHandler.postDelayed(callingElapsedRunnable, 1000);
    }

whatsapp notification timer my app notification

like image 484
propoLis Avatar asked Oct 11 '25 17:10

propoLis


1 Answers

I have added custom layout for <5.0 sdk versions:

< 5.0

public void startChronometer(String elapsedTime) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && builder != null) {
            RemoteViews mContentView = new RemoteViews(context.getPackageName(), R.layout.call_notification);
            //timerTxt
            TextView timerTxt = new TextView(context);
            timerTxt.setId(R.id.timerTxt);

            //callTypeTxt
            TextView callTypeTxt = new TextView(context);
            callTypeTxt.setId(R.id.callType);

            //calleeNameTxt
            TextView calleeNameTxt = new TextView(context);
            calleeNameTxt.setId(R.id.calleeName);

            mContentView.setTextViewText(timerTxt.getId(), elapsedTime);
            mContentView.setTextViewText(callTypeTxt.getId(), context.getString(R.string.call_in_progress));

            builder.setCustomBigContentView(mContentView);
            notificationManager.notify(CALL_NOTIFICATION_ID, builder.build());
        }
    }

other versions

builder.setUsesChronometer(true);

@Kishore Jethava Question:

Intent intent = new Intent(mContext, YourActivity.class);
intent.putExtra("tappedTime", System.currentTimeMillis());

PendingIntent pendingIntent = PendingIntent.getActivity(mContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);