发送通知
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/send_notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="send_notify" />
</LinearLayout>
程序代码
package cn.fanchencloud.studyapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int NOTIFY_ID = 0x123;
private Button sendNoticeButton;
private String channelId = "channelId1";
private NotificationManager notificationManager;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
addNotificationChannel();
sendNoticeButton = findViewById(R.id.send_notify);
sendNoticeButton.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notify:
Log.d("MainActivity", "点击按钮!");
sendNotification("还不赶紧关注公众号", "点击查看详情!");
break;
default:
break;
}
}
private void sendNotification(String title, String content) {
Notification notification;
if (Build.VERSION.SDK_INT >= 26) {
notification = new Notification.Builder(MainActivity.this, channelId)
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.mipmap.ic_launcher_copy)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true)
.build();
} else {
notification = new NotificationCompat.Builder(MainActivity.this, channelId)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setSmallIcon(R.mipmap.ic_launcher_copy)
.build();
}
notificationManager.notify(NOTIFY_ID, notification);
}
public void addNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "fanchen_notify";
String description = "fanchen的通知渠道";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = null;
mChannel = new NotificationChannel(channelId, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.enableVibration(true);
notificationManager.createNotificationChannel(mChannel);
}
}
}
运行截图
![运行截图](https://i-blog.csdnimg.cn/blog_migrate/19f111576d767dd1f06cc52e53b8f1b4.png)