Android通知样式
在这篇文章中,我们已经讨论并实现了基本的通知。
在本教程中,我们将研究android通知的更多高级功能,并以不同方式设置通知样式。
Android通知样式
Android Notification具有许多强大的功能,如果正确使用它们,将大大提高用户体验和用户参与度。
下面列出了我们将在本Android Notification教程中介绍的一些值得注意的功能。
- 通知动作
- 抬头通知
- 大文字样式通知
- 大图样式通知
- 收件箱样式通知
- 邮件样式通知
Android通知包含三个主要组件:
- 小图标(通常是我们的应用徽标)
- 标题
- 内容文字
Android Nougat(API 24)的引入使通知有了新外观,如下所示。
现在,大图标出现在右侧。
通知标题旁边有一个箭头,可让我们展开,折叠通知。
在本教程中,我们将使用Android为我们提供的一些预定义超赞样式来设置通知的样式。
我们将详细介绍以下功能。
- Android通知动作
- 抬头通知
- 丰富的通知
Android通知动作
Android通知操作基本上是放在通知正文下方的按钮。
通知动作必须包含一个图标,一个标签以及一个当用户选择该动作时要触发的" PendingIntent"。
随着Android N的介绍,操作按钮中的图标被省略,以留出空间给其他组件。
牛轧糖前设备中的通知措施示例如下。
以下是Android N及更高版本中的通知动作示例.Android Nougat中的通知动作
抬头通知
随着Android Lollipop(API级别21)的引入,当设备处于活动状态(即设备已解锁且屏幕处于打开状态)时,通知可以显示在一个小的浮动窗口中(也称为平视通知)。
当您使用应用程序并接到电话时,通常会看到此类通知。
弹出一个称为抬头通知的小型浮动通知,其中包含通知动作以接受或者拒绝呼叫。
丰富的通知
Android允许我们通过引入Android L向我们的应用程序添加丰富的样式。
使用这些样式将使通知看起来比以往更具吸引力。
下面列出了许多应用程序中使用的一些已知样式,这些样式是不言自明的。
- BigTextStyle
- BigPictureStyle
- InboxStyle
- MessageStyle
我们知道可以使用箭头扩展Android N上的通知。
要扩展牛轧糖之前版本的通知,您可以用两根手指在通知上向下滑动。
并非所有的Android版本都支持上述样式。
如果Android操作系统不支持丰富样式,则该样式将被忽略。
现在,让我们进入本教程的业务范围,并开发一个具有上述所有功能的应用程序。
Android通知教程代码
下面给出了" activity_main.xml"的代码:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.theitroad.stylingnotifications.MainActivity"> <Button android:id="@+id/btnNotificationActions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NOTIFICATION ACTIONS" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.196" <Button android:id="@+id/btnHeadsUp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="HEADS UP NOTIFICATION" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnNotificationActions" <Button android:id="@+id/btnBigTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BIG TEXT STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnHeadsUp" <Button android:id="@+id/btnBigPictureStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BIG PICTURE STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnBigTextStyle" <Button android:id="@+id/btnInboxStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="INBOX STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnBigPictureStyle" <Button android:id="@+id/btnMessageStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MESSAGE STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnInboxStyle" </android.support.constraint.ConstraintLayout>
我们为将要讨论的每种通知类型添加了一个按钮。
下面给出了MainActivity.java的框架代码。
public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btnNotificationActions, btnHeadsUpNotification, btnBigTextStyle, btnBigPictureStyle, btnInboxStyle, btnMessageStyle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clearNotification(); btnNotificationActions = (Button) findViewById(R.id.btnNotificationActions); btnHeadsUpNotification = (Button) findViewById(R.id.btnHeadsUp); btnBigTextStyle = (Button) findViewById(R.id.btnBigTextStyle); btnBigPictureStyle = (Button) findViewById(R.id.btnBigPictureStyle); btnInboxStyle = (Button) findViewById(R.id.btnInboxStyle); btnMessageStyle = (Button) findViewById(R.id.btnMessageStyle); btnNotificationActions.setOnClickListener(this); btnHeadsUpNotification.setOnClickListener(this); btnBigTextStyle.setOnClickListener(this); btnBigPictureStyle.setOnClickListener(this); btnInboxStyle.setOnClickListener(this); btnMessageStyle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnNotificationActions: notificationActions(); break; case R.id.btnHeadsUp: headsUpNotification(); break; case R.id.btnBigTextStyle: bigTextStyleNotification(); break; case R.id.btnBigPictureStyle: bigPictureStyleNotification(); break; case R.id.btnInboxStyle: inboxStyleNotification(); break; case R.id.btnMessageStyle: messageStyleNotification(); break; } } public PendingIntent getLaunchIntent(int notificationId, Context context) { Intent intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("notificationId", notificationId); return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); } private void clearNotification() { int notificationId = getIntent().getIntExtra("notificationId", 0); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notificationId); } private void notificationActions() { //Logic goes here. } private void headsUpNotification() { //Logic goes here. } private void bigTextStyleNotification() { //Logic goes here. } private void bigPictureStyleNotification(); { //Logic goes here. } private void inboxStyleNotification() { //Logic goes here. } private void messageStyleNotification() { //Logic goes here. } }
clearNotification()
方法用于清除通知列中的所有现有通知。
方法getLaunchIntent()返回一个PendingIntent实例,当从通知中触发该实例时,它将重新启动应用程序。
在深入研究每种通知的实现之前,让我们定义一下BroadcastReceiver,如下所示。
package com.theitroad.stylingnotifications; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int notificationId = intent.getIntExtra("notificationId", 0); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notificationId); } }
使用定义如下的接收器更新AndroidManifest.xml
文件。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.theitroad.stylingnotifications"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".NotificationReceiver" android:exported="false" <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" <category android:name="android.intent.category.LAUNCHER" </intent-filter> </activity> </application> </manifest>
在通知中添加动作
private void notificationActions() { int NOTIFICATION_ID = 1; NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Notification Actions"); builder.setContentText("Tap View to launch our website"); builder.setAutoCancel(true); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.theitroad.local")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); }
在上面的代码中,我们在实例builder
上设置了各种样式。
setColor()设置通知图标,标题和操作按钮文本的自定义颜色。
addAction()用于在通知内容下方设置操作按钮。
它需要三个参数:图标,文本和PendingIntent实例。
setContentIntent()设置点击通知正文时将触发的PendingIntent。
在上面的代码中,我们仅添加了PendingIntent以重新启动该应用程序。
setAutoCancel(true)用于在单击通知时关闭该通知。
NotificationManager类用于显示通知。
下面给出了触发上述类型的通知时应用程序的输出。
注意:
单击"查看"按钮后,该网址会在浏览器中启动,但不会取消该通知。
单击" DISMISS"按钮时,通知已清除,但通知托盘保持打开状态。
单击通知内容后,该通知将被取消,并且该活动将重新启动。
这是调用getLaunchIntent()和clearNotification()方法的地方。
实施抬头通知
private void headsUpNotification() { int NOTIFICATION_ID = 1; NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.jd) .setContentTitle("Heads Up Notification") .setContentText("View the latest Swift Tutorial") .setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.theitroad.local/15126/swift-function")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, builder.build()); }
要将通知设置为抬头通知,需要在构建器实例上设置两个属性。
setDefaults(NotificationCompat.DEFAULT_ALL) setPriority(NotificationCompat.PRIORITY_HIGH)
刷单向通知会取消该通知。
如果未取消,则抬头通知将消失,并在状态列中变为标准通知。
BigTextStyle通知
private void bigTextStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Text Style"); builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getResources().getString(R.string.lorem_ipsum))); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); builder.addAction(android.R.drawable.ic_menu_send, "OPEN APP", launchIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); }
通过将样式设置为" NotificationCompat.BigTextStyle()",可以将通知定制为大文本样式通知。
要显示的字符串在方法bigText()中输入。
BigPictureStyle通知
private void bigPictureStyleNotification() { int NOTIFICATION_ID = 1; Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.bg); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Picture Style"); builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(pic)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); }
为了将BigPicture显示在通知中,样式设置为NotificationCompat.BigPictureStyle().bigPicture(bitmap))
。
具有上述通知类型的输出如下。
InboxStyle通知
private void inboxStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setStyle(new NotificationCompat.InboxStyle().addLine("Hello").addLine("Are you there?").addLine("How's your day?").setBigContentTitle("3 New Messages for you").setSummaryText("Inbox")); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); }
收件箱样式通知是通过使用新的NotificationCompat.InboxStyle()
样式设置的。
每条消息都放在方法addLine()
内。
所有消息的摘要文本都放在方法setSummaryText()中。
这种风格的setContentTitle()
被setBigContentTitle()
代替
邮件样式通知
消息样式是Android N引入的。
通常用于聊天。
private void messageStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Messages"); builder.setStyle(new NotificationCompat.MessagingStyle("Teacher").setConversationTitle("Q&A Group") .addMessage("This type of notification was introduced in Android N. Right?",0,"Student 1") .addMessage("Yes",0,null) .addMessage("The constructor is passed with the name of the current user. Right?",0,"Student 2") .addMessage("True",0,null)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); }
在上面的代码中," NotificationCompat.MessagingStyle(String)"包含一个表示当前用户的字符串(通常在聊天中是您!)。
每条消息都带有时间戳和发件人名称添加到方法addMessage()中。
如果发件人名称设置为null,则表示消息来自当前用户(您),并且该名称来自构造函数。
在MainActivity.java中添加上述所有方法将为我们提供以下代码。
package com.theitroad.stylingnotifications; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btnNotificationActions, btnHeadsUpNotification, btnBigTextStyle, btnBigPictureStyle, btnInboxStyle, btnMessageStyle; NotificationCompat.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clearNotification(); btnNotificationActions = (Button) findViewById(R.id.btnNotificationActions); btnHeadsUpNotification = (Button) findViewById(R.id.btnHeadsUp); btnBigTextStyle = (Button) findViewById(R.id.btnBigTextStyle); btnBigPictureStyle = (Button) findViewById(R.id.btnBigPictureStyle); btnInboxStyle = (Button) findViewById(R.id.btnInboxStyle); btnMessageStyle = (Button) findViewById(R.id.btnMessageStyle); btnNotificationActions.setOnClickListener(this); btnHeadsUpNotification.setOnClickListener(this); btnBigTextStyle.setOnClickListener(this); btnBigPictureStyle.setOnClickListener(this); btnInboxStyle.setOnClickListener(this); btnMessageStyle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnNotificationActions: notificationActions(); break; case R.id.btnHeadsUp: headsUpNotification(); break; case R.id.btnBigTextStyle: bigTextStyleNotification(); break; case R.id.btnBigPictureStyle: bigPictureStyleNotification(); break; case R.id.btnInboxStyle: inboxStyleNotification(); break; case R.id.btnMessageStyle: messageStyleNotification(); break; } } private void notificationActions() { int NOTIFICATION_ID = 1; builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Notification Actions"); builder.setContentText("Tap View to launch our website"); builder.setAutoCancel(true); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.theitroad.local")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); buildNotification(NOTIFICATION_ID); } public PendingIntent getLaunchIntent(int notificationId, Context context) { Intent intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("notificationId", notificationId); return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); } private void clearNotification() { int notificationId = getIntent().getIntExtra("notificationId", 0); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notificationId); } private void headsUpNotification() { int NOTIFICATION_ID = 1; builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.jd) .setContentTitle("Heads Up Notification") .setContentText("View the latest Swift Tutorial") .setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.theitroad.local/15126/swift-function")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); buildNotification(NOTIFICATION_ID); } private void bigTextStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Text Style"); builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getResources().getString(R.string.lorem_ipsum))); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); builder.addAction(android.R.drawable.ic_menu_send, "OPEN APP", launchIntent); buildNotification(NOTIFICATION_ID); } private void bigPictureStyleNotification() { int NOTIFICATION_ID = 1; Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.bg); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Picture Style"); builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(pic)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); buildNotification(NOTIFICATION_ID); } private void inboxStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Messages"); builder.setStyle(new NotificationCompat.InboxStyle().addLine("Hello").addLine("Are you there?").addLine("How's your day?").setBigContentTitle("3 New Messages for you").setSummaryText("Inbox")); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); buildNotification(NOTIFICATION_ID); } private void messageStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Messages"); builder.setStyle(new NotificationCompat.MessagingStyle("Teacher").setConversationTitle("Q&A Group") .addMessage("This type of notification was introduced in Android N. Right?", 0, "Student 1") .addMessage("Yes", 0, null) .addMessage("The constructor is passed with the name of the current user. Right?", 0, "Student 2") .addMessage("True", 0, null)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); buildNotification(NOTIFICATION_ID); } private void buildNotification(int NOTIFICATION_ID) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); } }