Reputation: 13
.setWhen(System.currentTimeMillis())
is code to set time in push notification android and the result is like 15.54
. But if I want to set time like this 15:54:02
with seconds time what should I do? Can you help me or provide me example source code?
This is my notification-manager source code.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.drawable.logo).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setSmallIcon(R.drawable.logo)
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults = Notification.DEFAULT_ALL;
int id = (int) System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
notificationManager.notify(id, notification);
}
Upvotes: 1
Views: 1680
Reputation:
You set or not millisecond with .setWhen() by default it get that.
Time format what you want to show like 15:54:02
24 Hour Format with second. There is no settings for time format NotificationCompat.Builder
class.
What ever you set the millisecond that convert to time to show in notification like 15:54
without second.
What you can do is use custom Notification view rather then default view.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String formattedDate = "";
formattedDate = sdf.format(new Date());
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.logo);
contentView.setTextViewText(R.id.title, title);
contentView.setTextViewText(R.id.text, message);
contentView.setTextViewText(R.id.time, formattedDate);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.home)
.setContent(contentView);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
int id = (int) System.currentTimeMillis();
notificationManager.notify(id, notification);
Layout..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="left|center_horizontal|center_vertical"
android:weightSum="4">
<RelativeLayout
android:id="@+id/layout"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="64dp"
android:padding="10dp" >
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView
android:textSize="13dp"
android:textColor="#000"
android:text="Testing"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
/>
<TextView
android:textSize="13dp"
android:textColor="#000"
android:text="Testing is awecome"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_below="@id/title"
/>
</RelativeLayout>
<TextView
android:textSize="13dp"
android:textColor="#000"
android:text="Testing"
android:id="@+id/time"
android:layout_width="0dp"
android:layout_weight="1"
android:paddingRight="20dp"
android:gravity="right|center_vertical|center_horizontal"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"/>
</LinearLayout>
Output like this ...
Upvotes: 1
Reputation: 103
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
String dateString = formatter.format(new Date(dateInMillis)));
Upvotes: -1
Reputation: 1008
for that, you need to convert that time "14:54:02" in milliseconds and set that result time in .setWhen()
Upvotes: 0
Reputation: 75629
But if i want to set time like this 15:54:02
You cannot have that with built-in notification layout. If you really need seconds, you need to have custom notification layout. But I strongly discourage that.
Upvotes: 1