Me estoy haciendo un problema de notificaciones en mi dispositivo

Honey Arora:

Yo estaba tratando de hacer una aplicación sencilla. Pero me estoy haciendo un simple problema como si no recibo las notificaciones en mi dispositivo que es pastel de Android. Hago un botón en un fragmento, y cuando hago clic en él hago un método para mostrar la notificación al usuario, pero cuando hago clic en el botón de mi no he recibido ninguna notificación. Así que si alguien encuentra un problema en mi código por favor, respóndeme. Aquí está mi código de abajo: - Fragmento principal: -

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());
    }
}

fragment.xml actividad: -

<?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>

Gracias por leer mi pregunta por favor me responda si alguien encuentra un problema.

Mamba negro :

Prueba esto su trabajo correctamente

 //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());
        }

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=332684&siteId=1
Recomendado
Clasificación