How do I call a method when I click on a notification?

6

I wanted to know how to call a method when I click on a notification, I do not 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){}
}

asked by anonymous 22.07.2014 / 07:59

2 answers

1

You can use a BroadcastReceiver to do this.

Set 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 BroadcastReceiver derived class in your 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 BroadcastReceiver on the onResume method of Activity :

private ExecutaMetodoReceiver executaMetodoReceiver;

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

Do the "Unregister" method on onPause

@Override
protected void onPause() {

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

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

    
22.07.2014 / 14:02
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 submitting:

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

On receiving:

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

Treatment:

if(valorDoExtra.equals("valorDoExtra")) {
    metodo();
}
    
22.07.2014 / 13:16