почему Android 8.0 Oreo версия не вызывает уведомление - PullRequest
0 голосов
/ 24 октября 2018

Я сделал часть уведомлений в моей разработке мобильного приложения.Он отлично работает как в версиях KitKat, так и в верхних версиях, но в версии Oreo.Версия Oreo это не вызывает уведомление.в чем причина .. Ниже приведены мои коды ..

Активность оповещения

  List<Time> times = new ArrayList<>();
    times.add(new Time(hour, mminute));

Intent intent = new Intent(this, MyBroadcastReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

for (Time time : times) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), broadcastCodeCus++, intent, 0);
    Calendar cal_alarm = Calendar.getInstance();
    cal_alarm.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour));
    cal_alarm.set(Calendar.MINUTE, Integer.valueOf(mminute));
    cal_alarm.set(Calendar.SECOND, 00);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);
        Toast.makeText(this, "Alarm > KITKAT & Alarm Set For: " + hour + " : " + mminute, Toast.LENGTH_SHORT).show();
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);
        Toast.makeText(this, "Alarm < KITKAT & Alarm Set For: " + hour + " : " + mminute, Toast.LENGTH_SHORT).show();
    }

    customText.append(broadcastCodeCus + ". " + time.hour + ":" + time.minute + "\n");
}

MyBroadcastReceiver

NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent repeating_intent= new Intent(context,secondActivity.class);
        repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


        PendingIntent pendingIntent= PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,CHANNEL_ID)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.drink)
                .setContentTitle(textTitle)
                .setContentText(textContent)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(textContent))
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)      // >=API level 21
                .setLights(Color.WHITE, 2000, 3000)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); // >=API level 21
        notificationManager.notify(100,mBuilder.build());
    }

1 Ответ

0 голосов
/ 10 декабря 2018

В Android Oreo необходимо использовать канал с вашим Построителем уведомлений, вы можете следовать следующему коду для канала уведомлений.

    // Sets an ID for the notification, so it can be updated.
int notifyID = 1; 
String CHANNEL_ID = "my_channel_01";// The id of the channel. 
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_notify_status)
            .setChannelId(CHANNEL_ID)
            .build();
...