Error sending Java android notification

Asked

Viewed 17 times

-1

I’m trying to send notification on an android app. However, when I click the button to call the method, nothing happens, no error.

Follows code:

PERMISSION IN THE MANIFESTO:

 <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"></uses-permission>

Code of the Activity

package com.example.atividade3_2408;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class cadastrarItem extends AppCompatActivity {


    public static final String NOTIFICATION_CHANNEL_ID = "canal_de_notificacao_id";
    public static final String CHANNEL_NAME = "Meu canal de comunicação";
    public static final int NOTIFICATION_ID = 111;
    EditText descricao, quantidade;
    MeuSQLite gerenciadorBancoDeDados;
    SQLiteDatabase bancoDeDados;
    Button botao_notificar;

    public ItemDAO dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cadastrar_item);

        descricao = (EditText) findViewById(R.id.idDescricao);
        quantidade = findViewById(R.id.idQuantidade);

        dao = new ItemDAO(this);

        final NotificationChannel notificationChannel;
        final NotificationManagerCompat notificationManagerCompat;
        final NotificationManager notificationManager;
        final Notification notification;

        if (Build.VERSION.SDK_INT >= 26) {
            notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.enableLights(true);
            notificationChannel.enableVibration(true);
            notificationChannel.setLightColor(Color.GREEN);
            //Vibrar dispositivo após receber a notificação a cada milissegundos indicados abaixo:
            notificationChannel.setVibrationPattern(new long[]{500, 500, 500});
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

            notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        /////////////////////////////////////////////
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notificationBuilder.setContentTitle("Título da notificação");
        notificationBuilder.setContentText("Parabéns você está exibindo uma notificação.");
        notificationBuilder.setSmallIcon(R.drawable.ic_launcher_background);
        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background));
        notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        //Limpar a notificação após clicá-la
        notificationBuilder.setAutoCancel(true);

        notification = notificationBuilder.build();

        notificationManagerCompat = NotificationManagerCompat.from(this);

        //Botão para exibir a notificação:
        botao_notificar = findViewById(R.id.botao);
        botao_notificar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notificationManagerCompat.notify(NOTIFICATION_ID, notification);
            }
        });
    }

public void Salvar(View view) {

        Item item = new Item();
        item.setDescricao(descricao.getText().toString());
        item.setQuantidade(Integer.parseInt(quantidade.getText().toString()));

        if (TextUtils.isEmpty(descricao.getText().toString()) || TextUtils.isEmpty(quantidade.getText().toString())) {
            Toast.makeText(this, "Preencha os campos!", Toast.LENGTH_LONG).show();
        } else {
            dao.inserir(item);

            Toast.makeText(this, "Item inserido com sucesso!", Toast.LENGTH_LONG).show();
        }
    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.listar:
                startActivity(new Intent(this, listarItens.class));
                break;
            case R.id.cadastros:
                startActivity(new Intent(this, cadastrarItem.class));
        }


        return super.onOptionsItemSelected(item);
    }
}

XML code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = ".cadastrarItem"
    android:background = "#ffffff"
    android:orientation = "vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nome do item:">
    </TextView>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/idDescricao"></EditText>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quantidade do item:">
    </TextView>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/idQuantidade"></EditText>

    <Button
        android:id="@+id/enviar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="Salvar"
        android:text="Enviar"></Button>

    <Button
        android:id = "@+id/listar"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center"
        android:text = "Listar"></Button>

    <Button
        android:id="@+id/botao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Notificacao"
      />
</LinearLayout>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.