package com.example.myapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btn1;
Button btn2;
final static String GROUPONE_KEY = "GROUPONE";
final static String GROUPTWO_KEY = "GROUPTWO";
NotificationManager mNotifyMgr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
notify1();
break;
case R.id.btn2:
notify2();
break;
}
}
public void notify1() {
//ChannelId为"1",ChannelName为"Channel1"
NotificationChannel channel = new NotificationChannel("1",
"Channel1", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true); //是否在桌面icon右上角展示小红点
channel.setLightColor(Color.GREEN); //小红点颜色
channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
mNotifyMgr.createNotificationChannel(channel);
int notificationId = 0x1234;
Notification.Builder builder = new Notification.Builder(this,"1"); //与channelId对应
//icon title text必须包含,不然影响桌面图标小红点的展示
builder.setSmallIcon(R.drawable.icon1)
.setContentTitle("111")
.setContentText("1111");
mNotifyMgr.notify(notificationId, builder.build());
//builder.setGroup(GROUPONE_KEY);
}
public void notify2() {
//ChannelId为"1",ChannelName为"Channel1"
NotificationChannel channel = new NotificationChannel("2",
"Channel2", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true); //是否在桌面icon右上角展示小红点
channel.setLightColor(Color.GREEN); //小红点颜色
channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
mNotifyMgr.createNotificationChannel(channel);
int notificationId = 0x1235;
Notification.Builder builder = new Notification.Builder(this,"2"); //与channelId对应
//icon title text必须包含,不然影响桌面图标小红点的展示
builder.setSmallIcon(R.drawable.icon2)
.setContentTitle("222")
.setContentText("2222");
mNotifyMgr.notify(notificationId, builder.build());
}
}
版权声明:本文为qiongqiong421122原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。