Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a non-activity method on Notification Click

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

like image 559
Ezio Avatar asked Oct 12 '16 10:10

Ezio


People also ask

How can we call method in activity from non activity class?

use interface to communicate with activity from non activity class. create colorChange() in interface and get the instance of interface in non activity class and call that method.

How can we call activity method from another activity?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

What is notify method in Android?

A notification is a message that Android displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.


2 Answers

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();
}
like image 128
David Wasser Avatar answered Oct 13 '22 03:10

David Wasser


 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    //notification callbacks here in activity
    //Call method here from non activity class.
    Classname.methodName();
}
like image 35
Ankit Kumar Avatar answered Oct 13 '22 03:10

Ankit Kumar