android中的横幅通知

1.这个是点击按钮notice,然后按钮发送一个横幅通知 ,如果不想实现的话,可以剪切Onclick里面的内容

notice = findViewById(R.id.notice);
notice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String id = “my_channel_01”;
String name = “我是渠道名字”;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
// 设置要跳转的页面
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
// 要传递到下一个页面的数据
intent.putExtra(“result”, “Main函数要传递的参数”);

PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);//延迟跳转
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(mChannel);
notification = new Notification.Builder(MainActivity.this, “default”)
.setChannelId(id)
// 设置标题
.setContentTitle(“中*五百万!!消息的标题”)
.setContentText(“消息的内容”)
.setSmallIcon(R.mipmap.ic_launcher)
// 点击横幅自动跳转
.setContentIntent(pendingIntent)
// 点击横幅自动消失
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, “default”)
.setContentTitle(“中*五百万!!消息的标题”)
.setContentText(“消息的内容”)
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setChannelId(id);
notification = notificationBuilder.build();
}
notificationManager.notify(111123, notification);
}
});
2.接受的页面
activity_notification.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout 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=”.NotificationActivity”>

<ImageView
android:id=”@+id/imageView2″
android:layout_width=”378dp”
android:layout_height=”379dp”
android:scaleType=”fitXY”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent”
app:srcCompat=”@mipmap/be” />

<TextView
android:id=”@+id/t1″
android:layout_width=”224dp”
android:layout_height=”69dp”
android:layout_marginStart=”28dp”
android:layout_marginLeft=”28dp”
android:layout_marginTop=”36dp”
android:gravity=”center”
android:text=”TextView”
android:textSize=”30dp”
android:textStyle=”bold”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/imageView2″ />
</androidx.constraintlayout.widget.ConstraintLayout>
NotificationActivity.java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

public class NotificationActivity extends AppCompatActivity {

private TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
Intent intent = getIntent();
String str = intent.getStringExtra(“result”);
Log.d(“Notification”,str);
t1=findViewById(R.id.t1);
t1.setText(str);

}
}

效果图

%title插图%num