【发布时间】:2018-03-31 17:25:46
【问题描述】:
通知没有显示在上面的 V26 API.Notification cshown on v25 但是当我在 v27 上检查它时,通知没有出现。我的代码 启动服务并创建通知。
内部维护 onCreate()
Intent intent1 = new Intent(this, NotificationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(this, intent1);
} else {
startService(intent1);
}
NotificationService 扩展服务
onCreateMethod 我调用方法 shownotification 将调用广播接收器。
@Override
public void onCreate() {
super.onCreate();
session = SessionManager.getInstance(getApplicationContext());
showNotification();
}
private void showNotification() {
if (session.isNotificationOn() && session.isLogged()) {
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
notificationIntent.putExtra(NotificationReceiver.CODE, NotificationReceiver.TYPE_START_CODE);
sendBroadcast(notificationIntent);
}
}
NotificationReceiver 类
在这个课程中,我构建了通知并通知要显示的通知。
@Override
public void onReceive(Context context, Intent intent) {
buildNotification(context);
}
内部 buildNotification() 方法
private void buildNotification(Context context) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(false)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle(context.getString(R.string.app_name)) .setContentText(context.getString(R.string.notification_pull_down_information))
.setShowWhen(false)
.setColor(Color.BLACK);
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = context.getString(R.string.app_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(mChannel);
}
manager.notify(notificatioCode, builder.build());
}
但在 v26 上没有出现上述通知。 请帮帮我。
【问题讨论】:
-
想必
buildNotification()有问题,比如没有设置通知通道,使用不了。您的问题没有显示buildNotification()的实现,因此我们无法对此发表评论。 -
@CommonsWare 我已经粘贴了 buildnotificatio 方法,请看。谢谢
标签: android notifications android-notifications android-notification-bar