How do I save notification?

0

I am in a situation that I need to save the notification even though it is open, at the time of reading the notification I check if it has a connection, if it has it open normally, if it does not, it sends a message stating that it has no connection, is that the notification has already been opened and it disappears, so I wanted it to be saved in that case until the user read with a connection. how do I do? I want before clicking on the notification it will check if it has a connection, if it does not play a msg, and the notification can not disappear. Code:

public void generateNotification () {

    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) {
    }
}

Activity to read:

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();
    }
    
asked by anonymous 09.10.2014 / 19:55

1 answer

1

It lacked clarity in your question. Do you mean Notification of android ?, if it is enough 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 re-launch the notification as soon as you click on it if you do not have a connection.

You can use PendingIntent , so 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

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

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

NOTE: BroadCast has only 10 seconds to run, if it passes, the application will have an ANR.

    
09.10.2014 / 21:47