Почему мое уведомление не работает на Android> 7? - PullRequest
0 голосов
/ 25 марта 2020

Мне нужна помощь с моим уведомлением. Это не работает на Android> 7. Я использую широковещательный приемник для повторения моего уведомления каждый день в 10 часов утра. Я хочу, чтобы мое уведомление работало с Android мин 19 до макс 29. Возможно ли это? Любая помощь, пожалуйста?

Получатель:

public class AlarmReceiverNewUser extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String userName = App.SP().getString(App.SHARED_PREFERENCES_USER_NAME, "");
        final String textes[] = {context.getResources().getString(R.string.textNotification1), context.getResources().getString(R.string.textNotification2),
                context.getResources().getString(R.string.textNotification3), context.getResources().getString(R.string.textNotification4),
                context.getResources().getString(R.string.textNotification5), context.getResources().getString(R.string.textNotification6),
                context.getResources().getString(R.string.textNotification7)};
        int idx = new Random().nextInt(textes.length);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent1 = new Intent(context, FreeCalmBreathActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        PendingIntent pendingIntentSecond = PendingIntent.getActivity(context, 42, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context). //Enlever issou si ça bug
                setSmallIcon(R.drawable.ic_logo).
                setContentIntent(pendingIntentSecond).
                setContentText(userName + (textes[idx])).setStyle(new NotificationCompat.BigTextStyle()
                .bigText(userName + (textes[idx]))).
                addAction(R.drawable.ic_check_black_24dp, App.getContext().getResources().getString(R.string.Access), pendingIntentSecond).
                setContentTitle(App.getContext().getResources().getString(R.string.app_name)).
                setSound(alarmSound).
                setNumber(7).
                setChannelId(String.valueOf(42)).
                setAutoCancel(true);
        notificationManager.notify(42, builder.build());
        setNotification(context, 42);
    }

    private void setNotification(Context context, int id) {
        Calendar calender = Calendar.getInstance();
        calender.set(Calendar.HOUR_OF_DAY, 10);
        calender.set(Calendar.MINUTE, 0);
        calender.set(Calendar.SECOND, 0);

        Intent intent = new Intent(context, AlarmReceiverNewUser.class);
        intent.putExtra("NotificationId", id);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis() +
                AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

Ответы [ 2 ]

0 голосов
/ 25 марта 2020

Вам нужно создать канал уведомлений, чтобы видеть уведомление в N +

0 голосов
/ 25 марта 2020

Вам необходимо создать канал с помощью Notification Builder для отправки уведомления в Android O

NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 mNotificationManager.createNotificationChannel(mChannel);

Или вы можете отправить уведомление, установив android версия

NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
       mNotificationManager.createNotificationChannel(mChannel);
    }

Или Вы также можете установить идентификатор канала

NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, Name, Importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
            .setContentTitle("")
            .setContentText("")
            .setChannelId(CHANNEL_ID)
            .build();

Для более подробной информации, вы можете проверить Официальный Android Do c

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...