Notification入门(一)---简易Demo

创建Notification Channel

NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);

//channelId指的是Channel的id,我们可以自己定义,类型为字符串
//channelName指的是Channel的Name,我们也可以自己定义,类型为字符串
/*
importance指的是通知的重要程度即通知的优先级,有下面几种:
IMPORTANCE_NONE:不提示,不展示
IMPORTANCE_MIN:不提示,在通知下拉栏会展示,但是是收起的
IMPORTANCE_LOW:会在状态栏中显示,但不会弹窗,通知下拉栏会展示
IMPORTANCE_DEFAULT:会在状态栏中显示,允许有声音提示,但不会弹窗,通知下拉栏会展示

IMPORTANCE_HIGH:会弹窗提示,允许有提示音
IMPORTANCE_MAX:会弹窗提示,允许有提示音,可以使用全屏
*/

通过上面代码段的操作,就在系统中为本应用创建了一个消息Channel

弹出通知

/*
首先获得通知管理器的对象
new NotificationCompat.Builder(MainActivity.this,"chat1")
MainActivity.this是上下文对象
"chat1"是指定的消息Channel,我们在创建Channel模块定义的

setContentTitle("Mr Lei")     用于设置通知内容的标题
setContentText("Are you ok?")   用于设置通知内容
setWhen(Sytemt.currentTimeMillis()) 通知的发布事件,这儿设置的是系统收到该通知的事件
setSmallIcon(R.mipmap.people)  设置小图标 **此项不需写**
setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.people))  设置大图标
setAutoCancel(true).build(); 是否点击小时

mNotificationManager.notify(1, mNotification);  发布通知
1 整形数据,是用于唯一标识通知的,我们自行设置
mNotificaton Notification对象
*/
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotification = new NotificationCompat.Builder(MainActivity.this, "chat1")
	              .setContentTitle("Mr Lei")
	              .setContentText("Are you ok?")
	              .setWhen(System.currentTimeMillis())
	              .setSmallIcon(R.mipmap.people)
	              .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.people))
	              .setAutoCancel(true).build();  
mNotificationManager.notify(1, mNotification);

简易的通知Demo

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    NotificationManager mNotificationManager;
    Notification mNotification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initChannel();

        initWidget();
    }

    private void initChannel() {
        String channelId1 = "chat1";
        String channelName1 = "IMPORTANCE_DEFAULT";
        int importance1 = NotificationManager.IMPORTANCE_DEFAULT;
        createNotificationChannel(channelId1, channelName1, importance1);
    }

    private void initWidget() {
        Button button1 = findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                mNotification = new NotificationCompat.Builder(MainActivity.this, "chat1")
                        .setContentTitle("Mr Lei")
                        .setContentText("Are you ok?")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.people)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.people))
                        .setAutoCancel(true).build();
                mNotificationManager.notify(1, mNotification);
                break;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值