NOTIFICATION

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void showNotification(View view){
//        1.获得通知管理器

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        2.建立一个通知,8.0后需自建通知通道
        Notification notification = null;
        String id = "mchannel";
        String name = "通道1";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mchannel = new NotificationChannel(id,name,NotificationManager.IMPORTANCE_LOW);

            notificationManager.createNotificationChannel(mchannel);

            notification = new Notification.Builder(this,id)
                    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.drawable.icon))
                    .setSmallIcon(R.drawable.ic_baseline_event_note_24)
                    .setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.icon)))
                    .setContentTitle("好消息")
                    .setContentText("下周放假不上班")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)

//        3.绑定对应的activity
                    .setContentIntent(PendingIntent.getActivity(this,1,new Intent(this,NotificationResult.class),PendingIntent.FLAG_MUTABLE))
                    .build();
        }

        else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
            notification = new Notification.Builder(this)
                    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.icon))
                    .setSmallIcon(R.drawable.ic_baseline_event_note_24)
                    .setContentTitle("好消息")
                    .setContentText("下周放假不上班1")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentIntent(PendingIntent.getActivity(this, 1, new Intent(this, NotificationResult.class), PendingIntent.FLAG_CANCEL_CURRENT))
                    .build();
        }

//        4.发出通知
        notificationManager.notify(0,notification);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值