0

I'm trying to create a simple app to give notification daily at specific time but somehow it doesn't work and it return no error at all the code:

The button where the notification should start when it pressed:

setRenider.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,18);
            calendar.set(Calendar.MINUTE,14);
            calendar.set(Calendar.SECOND,0);

            Intent intent = new Intent(getApplicationContext(),Notification_reciever.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

        }
    });

then this is the notification class:

public class Notification_reciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent repeating_intent = new Intent(context,ReaptingActivity.class);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_free_breakfast_black_24dp)
            .setContentTitle("Water Time").setContentText("Drink Water").setAutoCancel(true);

    notificationManager.notify(100,builder.build());


  }
}

and I create a simple empty activity named it ReaptingActivity and I type those code in the manifests:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

<application
    <receiver android:name=".Notification_reciever"/>
</application>

Based on a tutorial I followed it should work but it havent. Note, the time I type is correct but it show no notification !!!

AbdallahRizk
  • 818
  • 1
  • 8
  • 18
  • 1
    Have you debugged to determine if the Receiver is running at all? Apart from that, starting with Oreo, `Notification`s need `NotificationChannel`s created before they're posted: https://stackoverflow.com/q/43093260. – Mike M. Nov 18 '19 at 02:23

1 Answers1

0

I just need it to add a channel on the notification class onRecieve method. This is how I create the channel:

    int notificationId = 1;
    String channelId = "channel-01";
    String channelName = "Channel Name";
    int importance = NotificationManager.IMPORTANCE_HIGH;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(
                channelId, channelName, importance);
        notificationManager.createNotificationChannel(mChannel);
    }

Then on the notification compat builder I need it to pass the channel

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
AbdallahRizk
  • 818
  • 1
  • 8
  • 18