public class MainActivity extends Activity {
Button btnNotification;
String channelID = "1";
String channelName = "channel_name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNotification = (Button) findViewById(R.id.notification);
btnNotification.setOnClickListener(new View.OnClickListener() {//使用内部类监听方法会报错,这是Android系统版本的问题
@Override
public void onClick(View v) {
// NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
// //NotificationManager manager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
// manager.createNotificationChannel(channel);
// Notification.Builder builder =new Notification.Builder(MainActivity.this);
// builder.setContentText("setContentText");
// builder.setContentTitle("title");
// builder.setSmallIcon(R.drawable.btn_check_on_selected);
// builder.setLargeIcon(loadBitmap(R.drawable.largeicon));
// builder.setContentInfo("补充内容");
// //创建通知时指定channelID
// builder.setChannelId(channelID);
// Notification notification = builder.build();
// manager.notify(1, notification);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(MainActivity.this.getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 通知渠道的id
String id = "my_channel_01";
// 用户可以看到的通知渠道的名字.
CharSequence name = getString(R.string.channel_name);
// 用户可以看到的通知渠道的描述
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
//注意Name和description不能为null或者""
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// 配置通知渠道的属性
mChannel.setDescription(description);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
mChannel.enableLights(false);
mChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
mChannel.enableVibration(false);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//最后在notificationmanager中创建该通知渠道
notificationManager.createNotificationChannel(mChannel);
builder.setChannelId(id);
}
builder.setContentInfo("补充内容");
builder.setContentText("setContentText");
builder.setContentTitle("title");
builder.setSmallIcon(R.drawable.btn_check_on_selected);
builder.setLargeIcon(loadBitmap(R.drawable.largeicon));
builder.setTicker("新消息");
//builder.setAutoCancel(true);
builder.setWhen(System.currentTimeMillis());
Intent intent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notificationManager.notify(1, notification);
}
});
// btnNotification.setOnClickListener(new View.OnClickListener(){});
}
private Bitmap loadBitmap(int id) {
final BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(id);
return Bitmap.createBitmap(bd.getBitmap());
}
}