0

Solved. This question was resolved by Diego D.

This code launch a Notification by a function: Notification(final String mytext). We get that Notification with ticker, title, text, autocancel,... when tap on it, run other function: My_Function(mytext);

To work, it doesn't need modify AndroidManifest because use Broadcasts.

public void Notification(final String mytext) { 
final Intent notifIntent = new Intent("action_call_method");
PendingIntent pendingmyIntent = PendingIntent.getBroadcast(context, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);

BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
              if (intent.getAction().equals("action_call_method")) {
                My_Function(mytext);
            } 
        }
    };

IntentFilter filter = new IntentFilter("action_call_method");
context.registerReceiver(receiver, filter);

        Notification noti = new Notification.Builder(context)
         .setTicker(ticker)
         .setContentTitle(title)
         .setContentText(text)
         .setSmallIcon(R.drawable.ic_lock_silent_mode_off) 
         .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
         .setAutoCancel(true)
         .setContentIntent(pendingmyIntent)
         .build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
    }
canel
  • 81
  • 2
  • 7
  • Possible duplicate of [How to set click listener for notification?](https://stackoverflow.com/questions/7184963/how-to-set-click-listener-for-notification) – ajankenm Jan 06 '18 at 23:07
  • This is not duplicate because in this post needs run Other function in same class file and doesn´t use AndroidManifest – canel Jan 09 '18 at 22:43
  • @canel You're welcome ! By the way I just renamed myself because that was a very old stack account haha. *-previously Theodore M.* – Diego D. Jan 11 '18 at 08:07

1 Answers1

0

Set a custom action for the Intent in your PendingIntent and register a receiver to it, then you'll be able to do what you want:

Intent notifIntent = new Intent("action_call_method");
PendingIntent pendingmyIntent = PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Then register a BroadcastReceiver

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("action_call_method")) {
                // Call your method here
            }
        }
    };

    IntentFilter filter = new IntentFilter("action_call_method");
    registerReceiver(receiver, filter);

Don't forget to unregister your receiver.

Diego D.
  • 398
  • 3
  • 8