How to call a method when clicking a notification?

Asked

Viewed 735 times

6

I wanted to know how to call a method by clicking on a notification, I don’t want it to call a Activity, but only a method that has within the same class.

Example:

public void gerarNotificacao(){

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setTicker("Ticker Texto");
    builder.setContentTitle(" 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){}
}

Notice that then he calls one Activity when clicking, I want you to call a method of the same class, as I do?

2 answers

5

Put some extra in the Intent, so when your Activity receives the Intent and you recognize that specific extra can do the action you want. In that case call your method.

When sending:

Intent intent = new Intent(this, Classe.class);
intent.putExtra("chaveDoExtra","valorDoExtra");

On receiving:

Intent intent = getIntent();
String valorDoExtra= myIntent.getStringExtra("chaveDoExtra");

Treating:

if(valorDoExtra.equals("valorDoExtra")) {
    metodo();
}

1


You can use a Broadcastreceiver to do this.

Define your Pendingintent of the notification as follows:

Intent notificationIntent = new Intent("CHAMAR_METODO_X");
PendingIntent btLocationPendingIntent = 
    PendingIntent.getBroadcast(context, 1234, notificationIntent,0);

.....
//resto do código para gerar a notificação
.....  

Define a class derived from Broadcastreceiver in his Activity :

public class ExecutaMetodoReceiver extends BroadcastReceiver 
{

    @Override
       public void onReceive(Context context, Intent intent) 
       {    
           String action = intent.getAction();
           if(action.equalsIgnoreCase("CHAMAR_METODO_X")){
               //Chame o seu método    
               aSuaActividade.metodoX();
           }
       }

}

Register the Broadcastreceiver in the method onResume of Activity:

private ExecutaMetodoReceiver executaMetodoReceiver;

@Override
protected void onResume(){
    super.onResume();
    registerReceiver(executaMetodoReceiver, new IntentFilter("CHAMAR_METODO_X"));
}

Do the "Unregister" in the method onPause

@Override
protected void onPause() {

    unregisterReceiver(executaMetodoReceiver);
    super.onPause();
}

Note: Your Activity must be running in order to receive the notification.

  • Your tip worked, thank you

Browser other questions tagged

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