0

I am trying to send a notification that appears under the top navigation bar as follows:

enter image description here

My codes work for android emulator with API < 26, but they do not work for android emulator with API >= 26. I found that it is because I need to add channel, so I did. However, although the codes compile well and go through all loops, and there is no error in logcat. Still, the notification does not appear on the screen.

Any help will be greatly appreciated. FYI, here is my FragmentAlarm.java:

package com.example.dailybible3;

import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import java.util.Calendar;

public class FragmentAlarm extends Fragment {
    private View view;
    private CheckBox box_sun, box_mon, box_tue, box_wed, box_thu, box_fri, box_sat;
    private CheckBox order_history, order_bible;
    private CheckBox ninety_days, one_year;
    private Button btn_save;

    public static FragmentAlarm newInstance() {
        FragmentAlarm fragmentAlarm = new FragmentAlarm();
        return fragmentAlarm;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_alarm, container, false);

        /* start of set notification */
        createNotificationChannel();

        /* end of set notification */

        //save button
        btn_save = (Button) view.findViewById(R.id.save_button);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String message = "This is a notificaiton example";
                NotificationCompat.Builder builder = new NotificationCompat.Builder(
                        getContext(), "notifyBibleVerse"
                )
                        .setSmallIcon(R.drawable.ic_bible_english)
                        .setContentTitle("Today's Verse")
                        .setContentText("Genesis 1 - 3")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                /* start of setting notification */
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("message", message);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(pendingIntent);

                NotificationManager notiifcationManager = (NotificationManager) getActivity().getSystemService(
                  Context.NOTIFICATION_SERVICE
                );
                notiifcationManager.notify(0, builder.build());

                toastMessage("Alarm is set!");
            }
        });

        return view;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "DailyBibleChannel";
            String description = "Channel for Bible Verse Reminder";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("notifyBibleVerse", name, importance);
            channel.setDescription(description);
        }
    }

    private void toastMessage(String message){
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }
}

gsk
  • 77
  • 6

1 Answers1

0

I am still not sure how to solve the above problem, but I found the working code from here: https://codinginflow.com/tutorials/android/alarmmanager. The major difference I see is that the working code has AlarmManager. Hope this help someone who is having troubles to set a notification.

gsk
  • 77
  • 6