3
  • I implemented notification in my Reminder Application.
    • As i needed custom notification, I used RemoteView for that and create layout for it.
    • In Layout i have used two textview and set content in it.
    • Notification showed here but with only one textview another is not showed here.

Code for Custom Notification

private void sendCustomNotification() {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, AlertActivity.class);
    notificationIntent.putExtra("reminder", reminderModel);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    RemoteViews notificationView = new RemoteViews(
            DateReceiverService.this.getPackageName(),
            R.layout.app_shortcut_activity
    );
    notificationView.setImageViewResource(
            R.id.imgAppIcon,
            R.mipmap.ic_launcher);
    notificationView.setTextViewText(R.id.txtReminder, "Hiii");//First Textview
    notificationView.setTextViewText(R.id.txtStartReminder, "Good");//Second Textview

    NotificationCompat.Builder builder = new NotificationCompat.Builder(DateReceiverService.this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker("HIiiiiiii")
            .setAutoCancel(false)
            .setContentIntent(contentIntent)
            .setContent(notificationView);


    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
    notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
    notification.defaults |= Notification.DEFAULT_SOUND; // Sound
    notification.flags = Notification.FLAG_ONGOING_EVENT;

    mNotificationManager.notify(notificationId, notification);
}

app_shortcut_activity.xml

<?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="wrap_content"
android:background="#8e8989"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp">

<ImageView
    android:id="@+id/imgAppIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#8e8989"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/txtReminder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtStartReminder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />
</LinearLayout>

<ImageView
    android:id="@+id/imgAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:padding="10dp"
    android:src="@drawable/icon_add" />

<ImageView
    android:id="@+id/imgSetting"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:src="@drawable/icon_setting_white" />
</LinearLayout>

Screenshot of Notification

Custom Notification

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56

0 Answers0