android studio開發運用手機多媒體
鏈接手機設備通知不起作用
學習安卓到通知這個模塊,建立專案鏈接手機后,發現發送通知手機上卻接收不到,
原因:安卓版本8.0以上使用通知需要先建立通知通道,
解決方案:
1.構造方法:
private String createNotificationChannel(String channelID, String channelNAME, int level) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
manager.createNotificationChannel(channel);
return channelID;
} else {
return null;
}
}
2.生成通知:
String channelId = createNotificationChannel("my_channel_ID","my_channel_name",NotificationManager.IMPORTANCE_MAX);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this,channelId)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
3.效果展示:
首先點擊按鈕發送通知

然后在下拉欄可以看到收到的通知了

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/319829.html
標籤:其他
