如何使用Notification的相关Style实现不同类型的Notification

xiaoxiao2021-02-28  69

Notification基本上是每个商用APP都会使用的一个用户交互控件(其实我也不是很清楚Notification到底是属于控件还是UI还是都包含了),好了,这不影响下面继续介绍如何正确的使用Notification!!!

好了,废话不多少,从这篇博客中您可以学到如何使用Notification,使用Notification的BigText样式、bigPicture样式、Inbox样式、如何使用mediaNotification、MessageNotification、带有回复功能的notification以及HeadUpNotification!!! 其中有些Notification的功能对于系统的版本存在要求,比带有回复功能的Notification要求SDK版本为24等!!! 先看一下Notification的知识点: Notification的相关Flag:

Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符 Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中) Notification.FLAG_INSISTENT //让声音、振动无限循环,直到用户响应 (取消或者打开) Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次 Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失 Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清除该通知 Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务

Notification的优先级:

Notification.PRIORITY_DEFAULT Notification.PRIORITY_HIGH Notification.PRIORITY_LOW Notification.PRIORITY_MAX Notification.PRIORITY_MIN

分别代表数字:-2 ~ +2,默认Notification的优先级为0;

现在开始一个一个介绍: 1、首先看一下最普通的Notification,只有三个元素: 1、SmallIcon —通知栏的Icon 2、ContentTitle — 通知栏的标题 3、ContentText — 通知栏的详细内容

效果图如下所示: 具体的设置代码如下:

public void showNormalNotification(View view) { Log.i("zyq", "showNormalNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); Notification n = mBuilder.build(); mNotificationManager.notify(99, n); }

在这里没有对Notification的Flag进行设置,需要的情况下可以设置Notification的Flag已达到我们想要的体验效果!

是不是很简单,如果你需要的只是一个提醒通知栏的话,可能连点击跳转的PendingIntent都不需要设置!

2、在上一Notification的基础上通过添加代码设置Style,完成一个BitTextNotification,这种类型的Notification在显示较长的文字提醒信息时可以用到,老规矩,还是看一下效果图吧!

效果感觉还不错,而且系统很贴心,设置了Style的通知栏,可以进行折叠,就跟gif显示的一样!!

实现代码:

public void showBigTextNotification(View view) { Log.i("zyq", "showBigTextNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); mBuilder.setPriority(Notification.PRIORITY_MAX); Notification.BigTextStyle mBigTextStyle = new Notification.BigTextStyle(); mBigTextStyle.setBigContentTitle("This is BigContentTitle"); mBigTextStyle.setSummaryText("This is BigSummaryText"); mBigTextStyle.bigText("This is BigText \n 如果。所有的伤痕都能够痊愈。 如果。所有的真心都能够换来真意。 如果。所有的相信都能够坚持。如果。所有的情感都能够完美。如果。依然能相遇在某座城。单纯的微笑。微微的幸福。肆意的拥抱。 该多好。可是真的只是如果。"); mBuilder.setStyle(mBigTextStyle); Notification n = mBuilder.build(); mNotificationManager.notify(98, n); }

在这里没有对Notification的Flag进行设置,需要的情况下可以设置Notification的Flag已达到我们想要的体验效果!

3、完成BigPictureNotification

通过名字就可以看出来这个样式的Notification跟BigTextNotification有很大的相似之处,在实现代码上没有多大的区别,只不过是传入的Style不同,先看一下效果图吧:

实现代码:

public void showBigPictureNotification(View view) { Log.i("zyq", "showBigPictureNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); mBuilder.setPriority(Notification.PRIORITY_MAX); Notification.BigPictureStyle mBigPictureStyle = new Notification.BigPictureStyle(); mBigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); mBigPictureStyle.bigLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round)); mBuilder.setStyle(mBigPictureStyle); Notification n = mBuilder.build(); mNotificationManager.notify(97, n); }

通过代码就可以看出,其实对于我们使用者来说是没有什么区别的,只是将BigTextStyle换成了BigPictureStyle,然后在Style填充相关的内容!

4、完成InboxNotification: 这个Notification前面的稍微有点区别,但是在只显示文字的时候,显示效果跟BigText有点相近! 先看一下效果图:

实现代码:

public void showInboxNotification(View view) { Log.i("zyq", "showInboxNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); mBuilder.setPriority(Notification.PRIORITY_MAX); Notification.InboxStyle mInboxStyle = new Notification.InboxStyle(); mInboxStyle.setBigContentTitle("This is Inbox BigContentTitle"); mInboxStyle.setSummaryText("This is SummaryText"); for(int i = 0;i<6;i++){ mInboxStyle.addLine("---------"+i); } mBuilder.setStyle(mInboxStyle); Notification n = mBuilder.build(); mNotificationManager.notify(96, n); }

这里需要注意一下,InboxStyle中,内容最多只能显示6行,当设置的line大于6行时,也只会显示6行,这是为什么呢? 因为在它的布局文件中定义了6个,可以看一下系统的layout文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/status_bar_latest_event_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:tag="inbox" > <include layout="@layout/notification_template_header" /> <LinearLayout android:id="@+id/notification_action_list_margin_target" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top" android:layout_marginTop="@dimen/notification_content_margin_top" android:clipToPadding="false" android:orientation="vertical"> <LinearLayout android:id="@+id/notification_main_column" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:paddingStart="@dimen/notification_content_margin_start" android:paddingEnd="@dimen/notification_content_margin_end" android:paddingBottom="@dimen/notification_content_margin_bottom" android:minHeight="@dimen/notification_min_content_height" android:clipToPadding="false" android:orientation="vertical" > <include layout="@layout/notification_template_part_line1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <include layout="@layout/notification_template_progress" android:layout_width="match_parent" android:layout_height="15dp" android:layout_marginTop="4dp"/> <TextView android:id="@+id/inbox_text0" android:textAppearance="@style/TextAppearance.Material.Notification" android:layout_width="match_parent" android:layout_height="0dp" android:singleLine="true" android:ellipsize="end" android:visibility="gone" android:layout_weight="1" /> <TextView android:id="@+id/inbox_text1" android:textAppearance="@style/TextAppearance.Material.Notification" android:layout_width="match_parent" android:layout_height="0dp" android:singleLine="true" android:ellipsize="end" android:visibility="gone" android:layout_weight="1" /> <TextView android:id="@+id/inbox_text2" android:textAppearance="@style/TextAppearance.Material.Notification" android:layout_width="match_parent" android:layout_height="0dp" android:singleLine="true" android:ellipsize="end" android:visibility="gone" android:layout_weight="1" /> <TextView android:id="@+id/inbox_text3" android:textAppearance="@style/TextAppearance.Material.Notification" android:layout_width="match_parent" android:layout_height="0dp" android:singleLine="true" android:ellipsize="end" android:visibility="gone" android:layout_weight="1" /> <TextView android:id="@+id/inbox_text4" android:textAppearance="@style/TextAppearance.Material.Notification" android:layout_width="match_parent" android:layout_height="0dp" android:singleLine="true" android:ellipsize="end" android:visibility="gone" android:layout_weight="1" /> <TextView android:id="@+id/inbox_text5" android:textAppearance="@style/TextAppearance.Material.Notification" android:layout_width="match_parent" android:layout_height="0dp" android:singleLine="true" android:ellipsize="end" android:visibility="gone" android:layout_weight="1" /> <TextView android:id="@+id/inbox_text6" android:textAppearance="@style/TextAppearance.Material.Notification" android:layout_width="match_parent" android:layout_height="0dp" android:singleLine="true" android:ellipsize="end" android:visibility="gone" android:layout_weight="1" /> </LinearLayout> <ViewStub android:layout="@layout/notification_material_reply_text" android:id="@+id/notification_material_reply_container" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <include layout="@layout/notification_material_action_list" /> <include layout="@layout/notification_template_right_icon" /> </FrameLayout>

我想这个layout就足以说明一切了!!

5、完成MediaNotification: 这个Style的Notification在音乐播放或者FMRadio的通知中会使用到,看一下实现的效果:

我没有设置相关的点击事件,这个点击按钮通过Notification.Action.Build进行实力构建的,详细情况还是看一下实现代码吧:

public void showMediaNotification(View view) { Log.i("zyq", "showMediaNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); mBuilder.setPriority(Notification.PRIORITY_MAX); mBuilder.setActions(new Notification.Action.Builder(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round), "1", null).build(), new Notification.Action.Builder(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher), "2", null).build(), new Notification.Action.Builder(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round), "3", null).build()); Notification.MediaStyle mMediaStyle = new Notification.MediaStyle(); mMediaStyle.setShowActionsInCompactView(0,1,2); mBuilder.setStyle(mMediaStyle); Notification n = mBuilder.build(); mNotificationManager.notify(95, n); }

这个Action的数量也是被源码写死的,在源码中可以看到,action的数量最多只能有3个!

private static final int MAX_ACTION_BUTTONS = 3; private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);

在添加Action的时候需要注意数量!

6、完成MessageNotification: 这里主要是使用这种类型的Notification来向用户显示短信的内容,在这里我就简单模拟一下! 效果图如下:

在这里模拟接收到10086的短信,实现代码如下:

public void showMessagingNotification(View view) { Log.i("zyq", "showMessagingNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); mBuilder.setPriority(Notification.PRIORITY_MAX); Notification.MessagingStyle mMessagingStyle = new Notification.MessagingStyle("zhuyuqiang"); mMessagingStyle.addMessage("Message Content", 10*1000, "sender"); mMessagingStyle.setConversationTitle("10086"); mBuilder.setStyle(mMessagingStyle); Notification n = mBuilder.build(); mNotificationManager.notify(94, n); }

这个Style的使用也很简单,跟BigText的有点类似,在这里就不详细说明了!

7、如何使用带有回复功能的Notification: 这个功能在通讯软件中应该很常用,但是这个功能要求系统的版本Android7,系统提供了方便的接口,我们直接调用即可,效果如下所示:

通知的实现代码:

public void showReplyStyleNotification(View view) { Log.i("zyq", "showCustomStyleNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); mBuilder.setPriority(Notification.PRIORITY_MAX); RemoteInput remoteInput = new RemoteInput.Builder("key_text_reply").setLabel("reply label").build(); Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher, getString(R.string.reply), PendingIntent.getService(MainActivity.this, 0,new Intent("start.notification.service"),0)) .addRemoteInput(remoteInput).build(); mBuilder.addAction(action); mBuilder.setGroup("hello"); Notification n = mBuilder.build(); mNotificationManager.notify(92, n); }

在代码中通过action启动一个service,在service中实现消息的发送,并取消或者更新当前的notification,如果不操作的话,Notification将永远保持发送状态!!

service代码:

package com.example.notificationdemo; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.RemoteInput; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.graphics.drawable.Icon; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.support.annotation.IntDef; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; import android.util.Log; import android.view.View; import android.widget.Toast; /** * Created by Administrator on 2017/4/27 0027. */ public class NotificationService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @RequiresApi(api = Build.VERSION_CODES.M) @Override public int onStartCommand(Intent intent, int flags, int startId) { Bundle data = RemoteInput.getResultsFromIntent(intent); Log.i("zyq","message content = "+data.getString("key_text_reply")); Toast.makeText(this,"Content : "+data.getString("key_text_reply"),Toast.LENGTH_LONG).show(); showReplyStyleNotification(intent); return super.onStartCommand(intent, flags, startId); } @RequiresApi(api = Build.VERSION_CODES.M) private void showReplyStyleNotification(Intent intent) { Log.i("zyq", "showCustomStyleNotification"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } NotificationManager manager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); manager.cancel(92); } }

这里涉及到的RemoteInput将在下篇博客中介绍,在这里就不做详细介绍了!

8、实现HeadUpNotification: 这个Notification我们可以在Android系统的来电通知中看到,也算是比较常见的notification了!看一下效果图:

效果也很明显(尽管不怎么美观),实现起来很简单,看一下详细代码:

public void showHeadUpNotification(View view) { Log.i("zyq", "showHeadUpNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setFullScreenIntent(mPendingIntent,true); mBuilder.setPriority(Notification.PRIORITY_MAX); mBuilder.setCustomHeadsUpContentView(new RemoteViews(getPackageName(),R.layout.heads_up)); Notification n = mBuilder.build(); mNotificationManager.notify(91, n); }

实现HeadUpNotification必须要设置setFullScreenIntent,否则将不去效果,有兴趣的朋友可以自己试一下! 这里我为了省事,就将点击事件设置为直接跳转到当前的activity中了!

9、实现自定义布局的Notification:

代码上的实现:

public void showCustomStyleNotification(View view) { Log.i("zyq", "showCustomStyleNotification"); Notification.Builder mBuilder = new Notification.Builder(MainActivity.this); mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round)); mBuilder.setContentTitle("this is Notification Title"); mBuilder.setContentText("this is Notification Text"); Intent mIntent = new Intent(); ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity"); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0); mBuilder.setContentIntent(mPendingIntent); mBuilder.setPriority(Notification.PRIORITY_MAX); RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.custom_layout_notification); mBuilder.setCustomContentView(remoteViews); Notification.DecoratedCustomViewStyle mCustomStyle = new Notification.DecoratedCustomViewStyle(); mBuilder.setStyle(mCustomStyle); Notification n = mBuilder.build(); mNotificationManager.notify(93, n); }

在这里我使用了DecoratedCustomViewStyle ,可以不使用这个style,实现自定义布局的Notification跟这个Style没有太多的关系,但是使用DecoratedCustomViewStyle 和DecoratedMediaCustomViewStyle必须要设置CustomContentView,你可以理解为是搭配使用的!

好了,关于Notification的相关Style就介绍到这里,写的不好不要喷我呀~

这是我的微信公众号,如果可以的话,希望您可以帮忙关注一下,这将是对我最大的鼓励了,谢谢!!

代码地址: 代码稍后会上传至GitHub中: 代码地址:https://github.com/zhuyuqiang2017/Other

转载请注明原文地址: https://www.6miu.com/read-42577.html

最新回复(0)