0

我正在尝试发送显示在顶部导航栏下方的通知,如下所示:

在此处输入图像描述

我的代码适用于 API < 26 的 android 模拟器,但不适用于 API >= 26 的 android 模拟器。我发现这是因为我需要添加通道,所以我做了。但是,尽管代码编译良好并经过所有循环,并且logcat. 尽管如此,通知并未出现在屏幕上。

任何帮助将不胜感激。仅供参考,这是我的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();
    }
}

4

1 回答 1

0

我仍然不确定如何解决上述问题,但我从这里找到了工作代码:https ://codinginflow.com/tutorials/android/alarmmanager 。我看到的主要区别是工作代码有 AlarmManager。希望这对设置通知有困难的人有所帮助。

于 2020-07-27T03:44:45.590 回答