在学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