Reputation: 1283
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);
}
Upvotes: 4
Views: 2928
Reputation: 26
I solved this using a very simple method. My initial notification
notification = new NotificationCompat.Builder(this, CALL_CHANNEL_ID)
.setContentText("Incoming Voice Call")
.setContentTitle(getDoctor().name)
.setSmallIcon(R.drawable.ic_baseline_call_received_24)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setShowWhen(true)
.setUsesChronometer(true)
.setFullScreenIntent(voicePendingIntent, true);
startForeground(1, notification.build());
store the timestamp when service started
public void startTimer() {
if (!isTimerRunning) {
startTime = System.currentTimeMillis();
isTimerRunning = true;
} else {
Log.e(TAG, "startTimer request for an already running timer");
}
}
every time you call elapsedTime(),(usually in the activity to get time), the notification gets updated.
public long elapsedTime() {
if (isTimerRunning()) {
durationMillis = System.currentTimeMillis() - startTime;
notification.setWhen(startTime);
notification.setOnlyAlertOnce(true);
notificationManager.notify(1, notification.build());
return (durationMillis);
} else {
return 0;
}
}
Upvotes: 0
Reputation: 1283
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);
Upvotes: 3
Reputation: 673
private void sendNotification(String from, String messageText) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(from)
.setContentText(messageText)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Upvotes: 2