Notification的简单实现方式
在开发中有时遇到简单的消息推送需求,这时我们不必使用第三方的消息推送,可以自己简单的实现。
Notification是显示在手机状态栏里的通知,位于手机的屏幕的上方,Notification所代表的是一种全局的通知,一般会通过NotificationManager来发送Notification,对于简单的推送需求就没必要使用第三方的推送也可以实现这个功能,如极光推送,等
NotificationManager是系统重要的服务,属于系统的框架层,应用程序可以通过NotificationManager 向系统发送全局消息。
在Android为Notification增加了Notification.builder()类,通过Builder模式获取到Notification的对象(Notification.builder().build()),能让开发者更轻松的获取到NOtification的对象;
Notification还提供了如下一些方法:
* setDefaults():设置通知LED灯,音乐,震动等;
* setAutocancel():设置通知后状态栏自动删除通知;
* setContentTitle():设置通知的标题
* setContentText():设置通知的内容;
* setSmalllcon():设置通知的图标;
* setLargerLcon():设置通知的大图标;
* setTick():设置通知在状态栏的提示文本;
* setContentintent():设置反击通知后将要启动的程序组件对应的PendingIntent。
发送一个Notification很简单,只需要按照如下的步骤进行就可以了:
1. 调用getSystemService(NOTIFICATION_SERVICE)获取到NotificationManager的服务;
2. 通过构造器创建一个Notification的对象(Notification.builder().build());
3. 为NotIfication设置各种相关的属性;
4. 通过NotificationManager来发送Notification
通过一个实例进行相关的介绍;
/*
* 首先要获取到Notificationmanager,在通过Notification.Builder().build()来获取到Notification的对象同时设置相关属性内容,相关的属性:标题,正文,图片,声音,消息来时提示的消息,最后获取消息后点击消息内容进行界面的跳转,最后通过NOtificationManger对象来进行消息的发送和取消;PendingIntent是一个延迟的Intent的触发,进行页面的跳转
*/
public class MainActivity extends AppCompatActivity {
private NotificationManager service;
private int NOTIFICATION_ID = 0x112;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
service = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
//设置点击事件发送消息
public void sendNotification(View view) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification notification = new Notification.Builder(this)
.setAutoCancel(true) //打开通知该通知自动的消失
.setTicker("有新的通知了") //设置通知的提示消息
.setContentTitle("重要的通知:") //通知的标题
.setContentText("您收到了重要的通知信息") //通知的正文内容
.setSmallIcon(R.mipmap.ic_launcher) //通知的图片信息
.setContentIntent(pi) //点击通知消息跳到另外一个界面
.build();
service.notify(NOTIFICATION_ID, notification);
}
//点击事件取消的通知的发送
public void cancelNotification(View view) {
service.cancel(NOTIFICATION_ID);
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.demo.myapplication.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:onClick="sendNotification"
android:text="发送通知"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:onClick="cancelNotification"
android:text="取消通知"
/>
</LinearLayout>
还可以自己设置提示的声音setSound(Uri.parse(“”));
设置自定义震动setVibrate(new long[]{0,50,100,150});
//要在manifest配置震动的权限
<uses-permission android:name="android.permission.VIBRATE"/>
还可以使用默认的设置声音,震动和闪光灯:
setDefaults()方法为Notification设置上面的效果:
* DEFAULT_SOUND :设置使用默认的声音
* DEFAULT_VIBRATE :设置使用默认的震动
* DEFAULT_LIGHTS :设置是使用默认的闪光灯
* ALL:设置使用默认的声音,震动和闪光灯