How to save notification?

Asked

Viewed 274 times

0

I am in a situation that I need to save the notification even if it is open, at the time of reading the notification I check if it has connection, if it has it opens normally, if it does not have it sends an msg informing that it has no connection, the problem is that the notification has already been opened and it disappears, so I wanted that in case it was saved there, until the user read with connection. How do I do? I want that before clicking on the notification it check if it has connection, if it has not played a msg, and the notification can not disappear. Code :

public void gerarNotificacao() {

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this,
            ReaderNotificacao.class), 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setLights(Color.WHITE, 500, 500);

    builder.setDefaults(0);
    builder.setTicker("Fadire");
    builder.setContentTitle("Fadire notificação");
    builder.setContentText("Você tem uma nova notícia");
    builder.setSmallIcon(R.drawable.icone);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
            R.drawable.icone));
    builder.setContentIntent(p);

    Notification n = builder.build();
    n.vibrate = new long[] { 150, 300, 150, 600 };
    n.flags = Notification.FLAG_AUTO_CANCEL;
    nm.notify(R.drawable.ic_launcher, n);

    try {
        Uri som = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone toque = RingtoneManager.getRingtone(this, som);
        toque.play();
    } catch (Exception e) {
    }
}

Reading activity:

if(verificaConexao())
lerNotificacoes();
    else {
        Toast.makeText(ReaderNotificacao.this, "Sem conexão, não é possivel ler", 1000).show();
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        finish();
    }
  • The Notification you refer to is Android Toast?

1 answer

1

Lacked clarity in your question. Do you refer to android Notification? , if it’s just you add

Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setOngoing(true) // o usuario não podera remover a notificaçao.

and to cancel the notification

NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);

now if you want the notification to be removed by the user, you simply relaunch the notification as soon as you click on it if you have no connection.

you can use Pendingintent, with this you can start an Activity, Broadcast or service depending on what you want, in this case you can use a Broadcast so it would look like this

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, 0);

In this Broadcast you put the check if you have internet, if you have you remove the notification.

OBS.: Broadcast has only 10 seconds to run, if you pass this, the application will have an ANR.

  • The part that I haven’t been able to do yet is, by clicking on the notification check the connection, that is how I do so that at the time of the notification click it checks if it has connection, if it has connection, it goes to another activity( what to read the notification) if it has no connection, an msg appears, but the notification cannot disappear. So I’ll put the code in so you can see it and tell me where to change it, thank you.

  • And then, I do this check in the other class, but the notification goes away, how do I not disappear, there in the other class ? the question is this

  • With Builder.setOngoing(true) it is not to disappear.

  • I edited, see how I added the code, that’s how?

Browser other questions tagged

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