0

I have written a code that is supposed to display notifications. I am choosing hour and minute from TimePicker component (notifications popping up every day at given time), then create an intent for Notification receiver. The database is updated with proper info and everything is being set with the AlarmManager. The request code ("code" variable) is unique for each notification. I am pasting code snippets below:

SettingActivity class:

findViewById(R.id.buttonNotification).setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            int hour, minute, id;
            Calendar calendar = Calendar.getInstance();
            PendingIntent pendingIntent;
            calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());

            Intent intent = new Intent(getApplicationContext(),Notifcation_receiver.class);
            hour = timePicker.getCurrentHour();
            minute = timePicker.getCurrentMinute();

            if(extra!=null){
                pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),extra.getInt("Code"),intent,PendingIntent.FLAG_UPDATE_CURRENT);
                intent.putExtra("Code",extra.getInt("Code"));
                db.updateNotification(hour,minute,extra.getInt("ID"));
            }
            else{
                int code= Notification.getID();
                intent.putExtra("Code",code);
                pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),code,intent,PendingIntent.FLAG_UPDATE_CURRENT);
                db.insertNotification(hour,minute);
            }
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
            startActivity(new Intent(SettingActivity.this, NotificationActivity.class));
        }
    });
}

Notification receiver class:

public class Notifcation_receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    int code = intent.getIntExtra("Code",0);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent repeating_intent = new Intent(context,MainActivity.class);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context,code,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.arrow_up_float)
            .setContentTitle("title")
            .setContentText("text")
            .setAutoCancel(false);

    notificationManager.notify(code,builder.build());
}

}

I can't seem to find the problem, causing the lack of planned notifications. Thank You for any help.

neo73
  • 434
  • 5
  • 20
Kuba M
  • 139
  • 12
  • 1
    What is your targetSdkVersion? If it is greater than 25, then check this [question](https://stackoverflow.com/questions/43093260/notification-not-showing-in-android-8-oreo/43093261). – Mehmed Oct 21 '17 at 22:57
  • Tested on API 26 (logcat log): "E/NotificationService: No Channel found for pkg=com.example.kuba.quizapp, channelId=null" – Kuba M Oct 22 '17 at 12:27
  • Thanks for a good tip! – Kuba M Oct 22 '17 at 12:27

1 Answers1

0

The problem was, as Mehmed mentioned, no channel set for the NotificationService.

Logcat log:

"E/NotificationService: No Channel found for pkg=com.example.kuba.quizapp, channelId=null"

It worked on API 22, but for 25 and higher the notification must be build with extra channel info.

Kuba M
  • 139
  • 12