Android 调试Notificaton示例代码

public class MainActivity extends Activity {

    Button btnNotification;
    String channelID = "1";
    String channelName = "channel_name";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnNotification = (Button) findViewById(R.id.notification);
        btnNotification.setOnClickListener(new View.OnClickListener() {//使用内部类监听方法会报错,这是Android系统版本的问题
            @Override
            public void onClick(View v) {
//                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//                NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
//                //NotificationManager manager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
//                manager.createNotificationChannel(channel);
//                Notification.Builder builder =new Notification.Builder(MainActivity.this);
//                builder.setContentText("setContentText");
//                builder.setContentTitle("title");
//                builder.setSmallIcon(R.drawable.btn_check_on_selected);
//                builder.setLargeIcon(loadBitmap(R.drawable.largeicon));
//                builder.setContentInfo("补充内容");
//                //创建通知时指定channelID
//                builder.setChannelId(channelID);
//                Notification notification = builder.build();
//                manager.notify(1, notification);
                
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(MainActivity.this.getApplicationContext());
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                      // 通知渠道的id
                      String id = "my_channel_01";
                      // 用户可以看到的通知渠道的名字.
                      CharSequence name = getString(R.string.channel_name);
                      // 用户可以看到的通知渠道的描述
                      String description = getString(R.string.channel_description);
                      int importance = NotificationManager.IMPORTANCE_HIGH;
                      //注意Name和description不能为null或者""
                      NotificationChannel mChannel = new NotificationChannel(id, name, importance);
                      // 配置通知渠道的属性
                      mChannel.setDescription(description);
                      // 设置通知出现时的闪灯(如果 android 设备支持的话)
                      mChannel.enableLights(false);
                      mChannel.setLightColor(Color.RED);
                      // 设置通知出现时的震动(如果 android 设备支持的话)
                      mChannel.enableVibration(false);
                      mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                      //最后在notificationmanager中创建该通知渠道
                      notificationManager.createNotificationChannel(mChannel);
                      builder.setChannelId(id);
                }
                builder.setContentInfo("补充内容");
                builder.setContentText("setContentText");
                builder.setContentTitle("title");
                builder.setSmallIcon(R.drawable.btn_check_on_selected);
                builder.setLargeIcon(loadBitmap(R.drawable.largeicon));
                builder.setTicker("新消息");
                //builder.setAutoCancel(true);
                builder.setWhen(System.currentTimeMillis());
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
                builder.setContentIntent(pendingIntent);
                Notification notification = builder.build();
                notificationManager.notify(1, notification);
            }
        });
//    btnNotification.setOnClickListener(new View.OnClickListener(){});
    }
    
    private Bitmap loadBitmap(int id) {
        final BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(id);
        return Bitmap.createBitmap(bd.getBitmap());
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值