场景

App在接收到后台推送的消息后,需要在系统通知栏中显示通知消息,并且点击通知消息跳转到新的页面,并将消息内容传递过去。

效果如下

 

Android中使用Notification在通知栏中显示通知_ide

注:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先在主页面添加一个按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">

<Button
android:id="@+id/showNotice"
android:text="显示通知"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</RelativeLayout>

然后在activity中设置按钮的点击事件

private Button showNoticeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

showNoticeButton = findViewById(R.id.showNotice);
showNoticeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showNotification(MainActivity.this, "通知内容:关注公众号,编程资料与技术共享学习");
}
});
}

在点击事件中调用了方法showNotification并且传递了消息的内容,在方法showNotification中

public void showNotification(Context context, String content) {
//1.创建通知管理器
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Android 8.0版本适配
NotificationChannel channel = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(context, "default");
} else {
builder = new NotificationCompat.Builder(context);
}
//用来进行跳转并传递消息的intent
Intent intent = new Intent(this, NoticeActivity.class);
intent.putExtra("content",content);
//2.创建通知实例
Notification notification = builder
.setContentTitle("通知标题:公众号-霸道的程序猿")
.setContentText(content)
.setWhen(System.currentTimeMillis())
//smallIcon 通知栏显示小图标
//android5.0 之后通知栏图标都修改了,小图标不能含有RGB图层,也就是说图片不能带颜色,否则显示的就成白色方格了
//解决方法一:为图片带颜色,targetSdkVersion改为21以下
//解决方法二:只能用白色透明底的图片
.setSmallIcon(R.mipmap.ic_launcher)
//LargeIcon 下拉后显示的图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
//收到通知时的效果,这里是默认声音
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT))
.build();
//3.notify
//notifyId每次要不一致,不然下一次的通知会覆盖上一次
int notifyId = new Random().nextInt();
notificationManager.notify(notifyId, notification);
}

首先创建通知管理器,然后创建通知实例,在创建通知实例时传递了一个Intent对象用来进行点击通知消息的跳转,通过

//用来进行跳转并传递消息的intent
Intent intent = new Intent(this, NoticeActivity.class);
intent.putExtra("content",content);

传递要显示的消息内容

然后新建一个Activity叫NoticeActivity用来实现点击通知后跳转显示消息内容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".NoticeActivity">

<TextView
android:id="@+id/content_TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</RelativeLayout>

添加一个TextView用来显示消息内容

然后在Activity中

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notice);
String content = getIntent().getStringExtra("content");
TextView textView = findViewById(R.id.content_TextView);
textView.setText("消息内容:\n"+content);
}

获取传递的参数并显示消息内容。

效果

点击显示通知按钮

 

Android中使用Notification在通知栏中显示通知_xml_02

点击通知内容

 

Android中使用Notification在通知栏中显示通知_xml_03