8

I have a java class MyClass which contains a method called callMethod. I want to call this method when user clicks on the notification

Below is the code i used to generate the notification

public class MainActivity extends AppCompatActivity {

    Button button;
    NotificationManager mNotifyMgr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        mNotifyMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
                Notification notification =
                    new NotificationCompat.Builder(MainActivity.this)
                            .setContentTitle("Notification")
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentText("Downloaded")
                            .setContentIntent(pendingIntent)
                            .build();

                mNotifyMgr.notify(1,notification);
            }
        });
    }
}

And below is the implementation of MyClass

public class MyClass {
    public void callMethod(){
        System.out.println("Notification clicked");
    }
}

Please help, I am stuck into this for a while now

Sajal Narang
  • 397
  • 1
  • 14
Ezio
  • 2,837
  • 2
  • 29
  • 45
  • try MyClass.callMethod() it will work, you can have an Util class to handle some generic cases like this – Babajide Apata Oct 12 '16 at 11:05
  • @BabajideApata where do i call the MyClass.callMethod()? – Ezio Oct 12 '16 at 11:08
  • looking at this line PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT);what it does is to call my class, so inside this class, you can call your method, immediately the class starts – Babajide Apata Oct 12 '16 at 11:12

2 Answers2

11

You could do something like this:

When creating your PendingIntent to put in the Notification:

Intent notificationIntent = new Intent(MainActivity.this, MyClass.class);
notificationIntent.putExtra("fromNotification", true);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,
         PendingIntent.FLAG_UPDATE_CURRENT);

Now, in MyClass.onCreate():

if (getIntent().hasExtra("fromNotification")) {
    callMethod();
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • MyClass is not an activity class, there is no onCreate() method in MyClass – Ezio Oct 13 '16 at 08:12
  • Well, then, you can't do this: `PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT); ` that you posted in your code! – David Wasser Oct 13 '16 at 13:04
  • 2
    A click on a `Notification` triggers an `Intent`. That's it. You need something to receive that `Intent` and perform the action you want to be done. Only Android components (`Activity`, `Service` or `BroadcastReceiver`) can receive an `Intent`. So it looks like you will need to write an `Activity`, `Service` or `BroadcastReceiver` that will receive the `Intent` and then call `MyClass.callMethod()`. – David Wasser Oct 13 '16 at 13:07
2
 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    //notification callbacks here in activity
    //Call method here from non activity class.
    Classname.methodName();
}
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38