Dynamic notifications, through parameters, on Android

Asked

Viewed 1,201 times

0

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

I’m actually doing it this way:

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 on 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 on 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 basing on this tutorial: http://www.androidbegin.com/tutorial/android-broadcast-receiver-notification-tutorial/

  • Why not arrow the message in theBroadcastReceiver? And recovers by Intent which is set in the onCreate?

  • A suggestion: include your question/problem in the question and not in the code. Otherwise the question can be closed.

  • Can give examples?

1 answer

0


Just as you pass parameters of BroadcastReceiver to the Activity of the notification, you can pass parameters to the BroadcastReceiver of Activity who 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 the onCreate of your BroadcastReceiver, you recover 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());
    }
}

Browser other questions tagged

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