为什么android 8.0 oreo版本不触发通知

时间:2018-10-24 15:58:51

标签: android-studio alarmmanager android-8.0-oreo

我在开发的移动应用程序中将通知部分制成。它在kitkat下版本和上层版本(但奥利奥版本)中均可完美运行。奥利奥版本,它不会触发通知。是什么原因。.以下是我的代码..

警报活动

  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 :(得分:0)

在Android Oreo中,必须在Notification Builder中使用频道 您可以按照下面的通知通道代码进行操作。

    // 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();