前一段時間看了一下android 8中的一些新特性,在組內分享了一下,想寫下來分享一下。 android 8 以前通知是沒法進行分類的,這樣用戶就沒法對本身感興趣和不感興趣的通知進行分類處理,致使用戶使用某些應用的時候,對一些推薦推銷的通知深受折磨。爲了進一步優化管理通知,Google在發佈android 8 時對通知作了修改優化,出現了通知渠道功能。html
這裏嘗試給通知渠道簡單下一個定義,每個通知都屬於一個通知渠道,開發者能夠在APP中自由建立多個渠道,須要注意的時,通知渠道一旦建立就沒法修改。android
應用程序中建立通知渠道(Notification Channel)的步驟:git
1.經過構造方法NotificationChannel(channelId, channelName, importance)建立一個NotificationChannel對象程序員
2.經過createNotificationChannel ( )來註冊NotificationChannel一個對象github
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "some_channel_id";
CharSequence channelName = "Some Channel";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
複製代碼
建立通知渠道須要三個參數api
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "upgrade";
String channelName = "升級";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelId, channelName, importance);
channelId = "compose";
channelName = "私信";
importance = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId, channelName, importance);
}
}
//建立通知渠道
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
public void sendUpgradeMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "upgrade")
.setContentTitle("升級")
.setContentText("程序員終於下班了。。")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setAutoCancel(true)
.build();
manager.notify(100, notification);
}
public void sendComposeMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "compose")
.setContentTitle("私信")
.setContentText("有人私信向你提出問題")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.build();
manager.notify(101, notification);
}
}
複製代碼
以前咱們說過,通知渠道一旦建立,控制權就在用戶手中,若是有一個重要通知渠道被用戶手動關閉了,咱們就要提醒用戶去手動打開該渠道。bash
getNotificationChannel()方法能夠獲取指定的通知渠道對象,ide
getNotificationChannels() 能夠獲取全部通知對象的集合,保存在一個list中優化
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = manager.getNotificationChannel("upgrade");
if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
startActivity(intent);
Toast.makeText(this, "升級通知不能關閉,請手動將通知打開", Toast.LENGTH_SHORT).show();
}
複製代碼
到此通知渠道的簡單使用介紹完畢, 參考ui
shoewann0402.github.io/2018/01/08/… developer.android.com/about/versi…