分类
Android Java

Android_Notification

Notification

一起学安卓

private NotificationManager manager;
private Notification notification;
private Intent intent;
private PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button=findViewById(R.id.button);

        intent = new Intent(MainActivity.this,MainActivity2.class);
        pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notification = new NotificationCompat.Builder(MainActivity.this, "YonC")
                .setContentTitle("阿离的通知")
                .setContentText("你有家吗?有温柔的女孩子在家里等你吗?")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setColor(Color.parseColor("#FFff0fff"))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        // 在安卓8.0以上需要使用NotificationChannel(通知渠道)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //三个参数,第一个就是notification对象对应的id
            //第二个是name,是用户收到通知时会看到的
            //第三个是重要性优先级
            NotificationChannel channel = new NotificationChannel("YonC", "YonC",
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
//button点击后触发
button.setOnClickListener
(
new View.OnClickListener() 
{
            @Override
            public void onClick(View view) 
{
                manager.notify(1,notification);
}
}
);

踩坑:setBigIcon()出现Cannot resolve method,我感觉应该是我当前的Android生产环境太低。其实不设BigIcon也罢。