Dynamic notifications, via parameters, on Android

0

I want to create a dynamic notification on Android, where I can change the title and text that is displayed, via parameters .

I'm currently doing this:

Home.class

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

    Intent i = new Intent(Home.this, BroadcastManager.class);
    sendBroadcast(i); //PASSAR COMO PARÂMETRO A MENSAGEM QUE EU QUERO QUE SEJA EXIBIDA
}

Pass parameters in this line sendBroadcast(i);

BroadcastManager.class

public class BroadcastManager extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent){
        Notification(context, "Mensagem"); // RECEBER AQUI, OS PARAMETROS PASSADOS
    }

    public void Notification(Context context, String message){
        String strtitle = context.getString(R.string.app_name);
        Intent intent = new Intent(context, NotificationView.class);

        intent.putExtra("title", strtitle);
        intent.putExtra("text", message);

        PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker(message)
                .setContentTitle("Titulo da notificação") //RECEBER AQUI, OS PARAMETROS PASSADO
                .setContentText(message)
                .setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

Receive in these lines .setContentTitle("Titulo da notificação") and .setContentText(message) the parameters passed

NotificationView.class

public class NotificationView extends Activity {
    String title;
    String text;
    TextView txttitle;
    TextView txttext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notificationview);

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Dismiss Notification
        notificationmanager.cancel(0);

        // Retrive the data from MainActivity.java
        Intent i = getIntent();

        title = i.getStringExtra("title");
        text = i.getStringExtra("text");

        // Locate the TextView
        txttitle = (TextView) findViewById(R.id.title);
        txttext = (TextView) findViewById(R.id.text);

        // Set the data into TextView
        txttitle.setText(title);
        txttext.setText(text);
    }
}
  

I relied on this tutorial: link

    
asked by anonymous 03.09.2014 / 17:23

1 answer

0

As you pass parameters from BroadcastReceiver to Activity of the notification, you can pass parameters to BroadcastReceiver of Activity you called.

In your code it would be:

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

    Intent i = new Intent(Home.this, BroadcastManager.class);

    String titulo = "SEU TITULO";
    String mensagem = "SUA MENSAGEM";

    i.putExtra("br_mensagem", mensagem);
    i.putExtra("br_title", titulo);

    sendBroadcast(i);
}

In%% of your onCreate , you can retrieve these parameters.

public class BroadcastManager extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent){
        // Recuperar parametro enviado da Activity
        String mensagem = intent.getStringExtra("br_mensagem");
        String titulo = intent.getStringExtra("br_titulo");

        Notification(context, titulo, mensagem);
    }

    public void Notification(Context context, String titulo, String message){
        String strtitle = context.getString(R.string.app_name);
        Intent intent = new Intent(context, NotificationView.class);

        intent.putExtra("title", strtitle);
        intent.putExtra("text", message);

        PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker(message)
            .setContentTitle(titulo)
            .setContentText(message)
            .setAutoCancel(true);

         NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
         notificationManager.notify(0, builder.build());
    }
}
    
03.09.2014 / 18:08