关于Notification的一系列问题(一)

1.作用:Notification使用非常普遍,几乎没个APP都会设置相关的功能,与推送相似,是与用户交互的重要方式。

2.用法:直接new Notification,再进行设置的方式目前已经不推荐使用了,现在应使用Builder来进行一系列的设置,基本方法及设置如下:

//发送通知

PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//1.初始化一个Builder对象
NotificationCompat.Builder notifyBuilder=new NotificationCompat.Builder(this);
//2.通过Builder进行一系列的set方法
notifyBuilder.setContentTitle("通知标题");
notifyBuilder.setContentText("通知内容");
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.map_3d_normal);
notifyBuilder.setSmallIcon(R.drawable.ic_launcher);
notifyBuilder.setLargeIcon(bitmap);
notifyBuilder.setNumber(1);
notifyBuilder.setWhen(System.currentTimeMillis());
notifyBuilder.setContentIntent(pendingIntent);
//3.其他设置
notifyBuilder.setAutoCancel(true);
notifyBuilder.setOngoing(true);
notifyBuilder.setTicker("通知来了");
NotificationManager mNotificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, notifyBuilder.build());

3.注意事项:setContentTitile   setContentText   setSmallIcon 这三个属性的设置是必须的,否则手机无法收到通知,PendingIntent的详解将在下一次解释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值