I plan to have a heads up notification that has two Actions ... one to Approve a login request and one to Decline a login request. By clicking on either of these actions I wish to fire off a HTTP request to my server and most importantly do not want to start a new Activity or have the user redirected to my app at all.
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.notificationicon)
.setContentTitle(notificationTitle)
.setContentText("Access Request for " + appName + " : " + otp)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(R.drawable.ic_tick, "Approve", someApproveIntent? );
Here is my notification builder and after looking around it seems that the addAction method is looking for a new/pendingIntent, which is confusing me as I cannot find any examples online where Intents do not lead to new Activities being fired off.
How would I implement some code ( a method maybe ) rather then starting a new Activity on each of my Actions...
Thanks for your help
If you don't want to start an activity you can also wrap a
BroadcastReceiver
or aService
directly in aPendingIntent
.Wherever you build your notification...
Your notification actions will start a service directly.
MyService.java
IntentServices run in a row one after another. They do the work on a worker thread.
AndroidManifest.xml
Don't forget to register the service in manifest.