java - 我的设备出现通知问题

标签 java android

我正在尝试制作一个简单的应用程序。但是我遇到了一个简单的问题,比如我没有在我的 Android pie 设备中收到通知。 我在一个 fragment 中制作了一个按钮,当我点击它时,我制作了一个向用户显示通知的方法,但是当我点击我的按钮时,我没有收到任何通知。因此,如果有人在我的代码中发现问题,请回答我。 下面是我的代码:- 主要 fragment :-

public class SearchFragment extends Fragment implements View.OnClickListener {

    View view;
    private Button clickme;
    private NotificationManagerCompat notificationManagerCompat;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search, container, false);
        clickme = view.findViewById(R.id.Click_me);
        notificationManagerCompat = NotificationManagerCompat.from(getActivity());
        clickme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Toast.makeText(getActivity(), "simple toast", Toast.LENGTH_SHORT).show();

                sendOnChannel1();
            }
        });
        return view;

    }

    @Override
    public void onClick(View view) {

    }

    public void sendOnChannel1() {
        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder)
                new NotificationCompat.Builder(getContext())
                        .setSmallIcon(R.drawable.ic_android_black_24dp)
                        .setContentTitle("Simple notification")
                        .setContentText("This is a test");


        NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(getContext().NOTIFICATION_SERVICE);
        notificationManager.notify(0, mBuilder.build());
    }
}

Activity fragment .xml:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="search"
        android:textColor="@color/ColorRed"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:textSize="20sp"/>


    <Button
        android:id="@+id/Click_me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me"
        />

</RelativeLayout>

感谢阅读我的问题,如果有人发现问题请回复我。

最佳答案

试试这个是否正常工作

 //send notification
private void sendNotification() {
            AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            assert audio != null;
            switch (audio.getRingerMode()) {
                case AudioManager.RINGER_MODE_NORMAL:
                    break;
                case AudioManager.RINGER_MODE_SILENT:
                    break;
                case AudioManager.RINGER_MODE_VIBRATE:
                    break;
            }

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true)
                    .setGroupSummary(true);

            // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                int importance = NotificationManager.IMPORTANCE_LOW;
                NotificationChannel notificationChannel = new NotificationChannel("chanelid", "Notification", importance);
                notificationChannel.enableLights(true);
                assert notificationManager != null;
                notificationBuilder.setChannelId("chanelid");

                if (notificationManager != null) {
                    List<NotificationChannel> channelList = notificationManager.getNotificationChannels();
                    for (int i = 0; channelList != null && i < channelList.size(); i++) {
                        notificationManager.deleteNotificationChannel(channelList.get(i).getId());
                    }
                }

                notificationManager.createNotificationChannel(notificationChannel);
            }
            notificationManager.notify(1, notificationBuilder.build());
        }

关于java - 我的设备出现通知问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57886694/

相关文章:

java - Android - 新 Intent 启动特定方法

android - 执行一段代码一次

java - 有没有办法用 jUnit Jupiter 测试列表/集合的顺序?

java - 对于强制比较类型的集合,抛出运行时异常比编译时检查有什么好处?

java - 多维 Arraylist Java

java - 单击 2 次鼠标绘制一条线

android - 如何覆盖(或隐藏)Android 库项目中定义的样式属性?

java - "No suitable Driver were found"出现此错误

android - 使用 Facebook android SDK ShareDialog。单击 shareddialog 的交叉按钮返回到带有 onSuccess 回调的应用程序

android - 如何设置长文本只显示2行并附加