什么是通知:
当某个程序希望向用户发送一些提示信息时,而该应用又不在前台运行,这时借助通知就可以轻松实现,当触发通知时,状态栏会显示一个小图标,下拉状态栏就可以看到通知的详细信息了。
实例讲解:
效果展现:
代码实现:(需要说明的是这个通知在Android8.0无法运行,我以后有时间会补充,我是在5.0中运行的)
1.修改activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.notificationtest.MainActivity">
<Button
android:id="@+id/send_notification_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send notification" />
</LinearLayout>
2.修改MainActivity中的代码,实现发送通知:
public class MainActivity extends AppCompatActivity {
private Button notifyBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notifyBtn = findViewById(R.id.send_notification_btn);
//对发送通知的按钮进行监听
notifyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建NotificationManager对象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//通过NotificationCompat.Builder()方法获取Notification对象
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("hello world")
.setContentText("i miss you ,you know?")
.setWhen(System.currentTimeMillis())
//设置小图标,就是点击通知按钮后,状态栏上的小白点
.setSmallIcon(R.mipmap.ic_launcher)
//下拉状态栏后,看到的图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
//最后发布通知
/**
* Post a notification to be shown in the status bar. If a notification with
* the same id has already been posted by your application and has not yet been canceled, it
* will be replaced by the updated information.
*
* @param id An identifier for this notification unique within your
* application. 设置ID,试你的通知唯一
* @param notification A {@link Notification} object describing what to show the user. Must not
* be null. 第二个参数就是Notification对象
*/
manager.notify(,notification);
}
});
}
}
over
但是,当我们点击这条通知时,并没用任何反应,这和现实中不一样,下面我们就来实现点击通知调转到相应的页面。
1.创建点击通知跳转的页面,然后需修改布局,让你能看出实现调转了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.notificationtest.NotificationActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is notification"
android:layout_gravity="center_horizontal"
android:textSize="25sp"/>
</LinearLayout>
2.修改MainActivity中不分代码:
........
notifyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建intent对象,表名intent的意图
Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
//创建PendingIntent对象,param1:当前环境,param2:通常0就行,param3:pendingIntent意图,param4:pendingIntent意图,一般填入0就行
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,,intent,);
//创建NotificationManager对象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//通过NotificationCompat.Builder()方法获取Notification对象
Notification notification = new NotificationCompat.Builder(MainActivity.this)
............
.setWhen(System.currentTimeMillis())
//点击通知后自动取消
.setAutoCancel(true)
//实现跳转
.setContentIntent(pendingIntent)
.........
.build();
//最后发布通知
...........
}
});
}
}
效果展现: