安卓通知

安卓通知

通知是程序在后台运行时候显示在系统栏的内容,可以使用在Service,Activity,content provider中

基本使用

(1)构建NotificationManager

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

getSystemService()方法是Activity里面的方法,非Activity使用就需要传递一下上下文了

(2)构建通知

Notification notification = new NotificationCompat.Builder(this, id)
.setContentTitle("this is title")
  .setContentText("this is text")
  .setSmallIcon(R.mipmap.ic_launcher_round)
  .build();

setContentTitle() setContentText() setSmallIcon()这三个是一个通知必备的,分别是标题正文以及通知在顶部冒出来的图标,通知中跟图片有关的传入的都是一个mipmap,图片不是的时候需要转换

BitmapFactory.decodeResource(getResources(),R.drawable.hh)

(3)解析 NotificationCompat.Builder

这个方法之前只有一个参数,后来被弃用了,但是网上好多人博客一大抄,还没更新。这个方法现在两个参数,第一个是context,第二个是channelId,如果channelId没构建一定运行不起来

那么构建channelId呢?

String id ="channel_1";//channel的id
String description = "123";//channel的描述信息
int importance = NotificationManager.IMPORTANCE_LOW;//channel的重要性
NotificationChannel channel = new NotificationChannel(id, "123", importance);//生成channel
notificationManager.createNotificationChannel(channel);

这些完成后讲id传入就可以了,直接传递”channel_1“也可以

(4)最后就是发通知过程

notificationManager.notify(1, notification); 第一个参数是通知的id,是唯一的,当想要点击后有通知消失的效果一种实现方式就是将这个id捉到,然后notificationManager.cancel(1);

(5)通知的点击效果

上面的实现完通知是不能点击的,我们要点击效果需要构建一个PendingIntent,他和Intent有相似的地方,也可以用来启动活动,启动服务,发送广播等,相当于一个延缓版本的Intent。

Intent intent=new Intent(MainActivity.this,NewActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

四个参数,第一个context,第二个一般用不到传入0,第三个是点击通知后的行为,可以是一个构建的Intent,第四个有几个可选值,一般传入0就可以

然后在build()之前传入.setContentIntent(pendingIntent);

(6)取消通知,可以在build()前面采用.setAutoCancel(true),还有一种方法就是

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(1);
Demo

新建一个Activity,在布局文件中设置一个按钮,用来控制发送通知。同时创建一个NewActivity,用来支持跳转

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.send_message);
        button.setOnClickListener(this);
    }
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.send_message:
                    //channnelId的构建
                    String id ="channel_1";//channel的id
                    String description = "123";//channel的描述信息
                    int importance = NotificationManager.IMPORTANCE_LOW;//channel的重要性
                    NotificationChannel channel = new NotificationChannel(id, "123", importance);//生成channel

                    //构建PendingIntent
                    Intent intent=new Intent(MainActivity.this,NewActivity.class);
                    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

                    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.createNotificationChannel(channel);
                    Notification notification = new NotificationCompat.Builder(this, "channel_1")
                            .setContentTitle("this is title")
                            .setContentText("this is text")
                            .setSmallIcon(R.mipmap.ic_launcher_round)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.hh))
                            .setWhen(System.currentTimeMillis())//通知发生的时间点  马上
                            .setContentIntent(pendingIntent)//支持通知栏点击操作,并进行相应动作
                            .setAutoCancel(true)//可以在点击后自动消失
                            //.setSound(Uri.fromFile(new File("/System/media/audio/ringtones")))//播放通知声音
                            //.setDefaults(NotificationCompat.DEFAULT_ALL)//将所有相关效果都设置为默认的
                            //.setVibrate(new long[]{0,1000,1000,1000})//震动
                            //.setLights(Color.GREEN,1000,1000)//灯闪烁时间,我这个使用不报错,但是也没看到效果,还有声音和震动都没看到
                            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.hh)))//大图风格
                            .setStyle(new NotificationCompat.BigTextStyle().bigText("人生若只如初见,何事秋风悲画扇,等闲变却故人心,却道故人心易变,骊山风雨清宵半,泪雨霖铃终不怨,何如薄幸锦衣郎,比翼连枝当日愿"))

                            //.setPriority(NotificationCompat.PRIORITY_MAX)
                            .build();
                    notificationManager.notify(1, notification);
                    break;
                default:
                    break;
            }
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值