NotificationManager 简单实例

 每次写Notification 都忘记怎么写,给出一个最简单的系统通知栏发通知的方法:

    private void sendNotification() {

        //设置要跳转的activity
        Intent intent = new Intent(  );
        intent.setClass( this, SDK_WebApp.class );
        PendingIntent activity = PendingIntent.getActivity( this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT );
        
        //获取NotificationManager实例
        NotificationManager notifyManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE);
        //实例化NotificationCompat.Builde并设置相关属性
        Notification builder = new NotificationCompat.Builder(this,"default")
                //设置小图标
                .setSmallIcon(R.drawable.icon)
                //设置通知标题
                .setContentTitle("最简单的Notification")
                //设置通知内容
                .setContentText("只有小图标、标题、内容")
                //设置延时Intent
                .setContentIntent(activity)
                //设置可以自动取消
                .setAutoCancel( true )
                //设置通知时间,默认为系统发出通知的时间,通常不用设置
                //.setWhen(System.currentTimeMillis());
                .build();

        notifyManager.notify(1, builder);
    }

 注意Notifivation 引入v4兼容包里面的:

import android.support.v4.app.NotificationCompat;

显示方法过时的原因:原因是升级到Android O 版本后,该方法被以下方法取代: 

NotificationCompat.Builder(Context context, String channelId)

而channelId的实际作用是将notification进行分类,如设置不同优先级等。也可使用NotificationChannel.getId()设置channelId 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值