Custom notification

I would like to know if there is a way to use the notification bar to perform some operations (onClick), without starting / resuming activity.

for example .. let's say I raise notifcation, and when the user clicks on it, instead of transferring me to some activity, it calls some regular method in my current activity / service

Is there any way to implement such a thing?

for example, the current notifcation code makes standard onClick behavior. launching an activity .. how will it bind the code to call some method instead of activity?

messagesManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, message, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, someActivity.class), 0); notification.setLatestEventInfo(context, "notification", message, contentIntent); messagesManager.notify(R.string.noto, notification); 
+2
source share
2 answers

Is there any way to implement such a thing?

Use the appropriate PendingIntent . Instead of calling getActivity() call getService() or getBroadcast() .

+3
source

I do not think this is possible, or at least best practice. Perhaps Android can kill your activity while the notification is still waiting in the top bar. For example, maybe you received a phone call, but Android has a low level of RAM - it kills your activity, so there really isnโ€™t any โ€œcurrentโ€ activity.

0
source

Source: https://habr.com/ru/post/1412353/


All Articles