在學Android多媒體Notification時,怎麼修改代碼,手機端就是不彈通知,翻看Log發現如標題錯誤,經在網上查資料,發現是手機設置問題,打開手機設置,通知與狀態欄,通知管理,找到對應的應用,允許該應用談通知即可。
代碼如下:
private void createNotification() {
Intent intent = new Intent(this, NotifictionActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, "1")
.setContentTitle("我的通知")
.setContentText("我通知的內容")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true)
.build();
mNotificationManager.notify(1, notification);
} else {
Notification notification = new Notification.Builder(this)
.setContentTitle("我的通知")
.setContentText("我通知的內容")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true).build();
mNotificationManager.notify(1, notification);
}
}
參考鏈接:link