1. 程式人生 > >Android O 以上及以下Notification寫法心得

Android O 以上及以下Notification寫法心得

Android O Notification 新增 Channel ,鼓搗半天之後總結一點心得。


private static final String mId = "1";//Channel ID

private static final String mName = "Channel_1";//Channel Name

NotificationManager manager =
                   (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //判斷安卓版本
                    NotificationChannel channel =
                            new NotificationChannel(mId, mName,         NotificationManager.IMPORTANCE_LOW);
                    manager.createNotificationChannel(channel);//安卓O以上建立Channel
                }
                Notification notification = new NotificationCompat.Builder(this,mId)
                        .setContentTitle("Title")//安卓O以下方法
                        .setContentText("Text")
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                        .setWhen(System.currentTimeMillis())
                        .setPriority(Notification.PRIORITY_MAX)
                        .setChannelId(mId)
                        .build();
                manager.notify(1,notification);