2020-06-20

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = findViewById(R.id.send_notice);
Button removeButton = findViewById(R.id.remove_notice);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notification notification = createForegroundNotification();
notificationManager.notify(123, notification);
}
});
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notification notification = createForegroundNotification();
notificationManager.cancel(123);
}
});
}

private Notification createForegroundNotification() {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // 唯一的通知通道的id.
    String notificationChannelId = "notification_channel_id_01";

    // 新建消息通道
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //用户可见的通道名称
        String channelName = "Foreground Service Notification";
        //通道的重要程度
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
        notificationChannel.setDescription("Channel description");
        //LED灯
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        //震动
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
    //通知小图标
    builder.setSmallIcon(R.drawable.ic_launcher_foreground);
    //通知标题
    builder.setContentTitle("前台服务");
    //通知内容
    builder.setContentText("前台服务正在运行");
    //设定通知显示的时间
    builder.setWhen(System.currentTimeMillis());
    //设定启动的内容
    Intent activityIntent = new Intent(this, NotificationActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    //添加按钮点击事件
    builder.addAction(R.mipmap.ic_launcher,"按钮",pendingIntent);

    //创建通知并返回
    return builder.build();
}

// private void showNotification(String ticker, String contentTitle, String contentText) {
// Notification notification;
// Intent it = new Intent(this, NotificationActivity.class);
// //(Context对象,Intent的id,Intent对象,对Intent的更新方式)
// //FLAG_UPDATE_CURRENT表示不销毁原来的PendingIntent,直接替换其中的Intent内容
// //FLAG_CANCEL_CURRENT表示取消当前的PendingIntent,重新创建新的PendingIntent对象
// PendingIntent pIntent = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
// //创建一个远程视图管理
// RemoteViews contentView = new RemoteViews(getApplication().getPackageName(), R.layout.notification_layout);
// contentView.setTextViewText(R.id.n_title, contentTitle);
// contentView.setTextViewText(R.id.n_content, contentText);//设置文本框中显示的文本内容
// contentView.setImageViewResource(R.id.n_icon, R.drawable.ic_launcher_background);//设置图片视图
// //设置自定义布局中的点击监听
// //关闭的Intent,MusicService 进入onStartCommand中
// Intent closeIntent = new Intent(this, NotificationActivity.class);
// closeIntent.putExtra("oop", 1);
// PendingIntent closePdIntent = PendingIntent.getService(this, 2, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// contentView.setOnClickPendingIntent(R.id.n_close, closePdIntent);
//
// if (Build.VERSION.SDK_INT >= 16) {
// Notification.Builder builder = new Notification.Builder(this);
// //显示在状态栏上的小图标
// builder.setSmallIcon(R.drawable.ic_launcher_background);
// //显示在状态栏上的提示文本(显示一段时间消失)
// builder.setTicker(ticker);
//// //下拉看到的内容标题和文本
//// builder.setContentTitle(contentTitle);
//// builder.setContentText(contentText);
//// //通知时间
//// builder.setWhen(System.currentTimeMillis());
// builder.setContent(contentView);
// //如果点击了内容部分,跳转到Activity01界面
// builder.setContentIntent(pIntent);
// //创建通知
// notification = builder.build();
// } else {
// notification = new Notification(R.drawable.timg, ticker, System.currentTimeMillis());
// notification.contentIntent = pIntent;
// notification.contentView = contentView;
// }
// //自动取消(当通知被点击时,系统会移除通知)
// notification.flags = notification.FLAG_AUTO_CANCEL;
// notification.defaults = Notification.DEFAULT_ALL;
// // 显示通知
// .notify(333, notification);
// }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。