custom notification button click - android

In my application I have a notification to show.
Let's say when notification is show I want press 'Yes' to go an Activity and hide notification, press 'No' do nothing just hide notification.
I tried this code but instead of onclick is onClckPendingIntent and I can't do anything that I want.
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_push_layout);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContent(remoteViews)
.setAutoCancel(true);
Intent intent = new Intent(this,GPSTrackingActivity.class);
final Intent yesIntent = new Intent(intent);
final Intent noIntent = new Intent(this, GPSTrackingActivity.class);
TaskStackBuilder yesStackBuilder = TaskStackBuilder.create(this);
yesStackBuilder.addParentStack(MainActivity.class);
yesStackBuilder.addNextIntent(yesIntent);
TaskStackBuilder noStackBuilder = TaskStackBuilder.create(this);
noStackBuilder.addParentStack(MainActivity.class);
noStackBuilder.addNextIntent(noIntent);
PendingIntent yesPendingIntent = yesStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent noPendingIntent = noStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_yes, yesPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.btn_no, noPendingIntent);
mNotificationManager.notify(100, mBuilder.build());
How could I do this ?

I know it's kinda late, but I managed to get things done via receiver.
you should:
create a receiver
create two intents to the receiver -
String SHOULD_OPEN = "should_open_intent"
Intent yes = new Intent(this, MyReceiver.class);
yes.putBooleanExtra(SHOULD_OPEN, true);
and same on Intent "no"
wrap them with PendingIntent
in you receiver, at the function "onReceive" get that data out using intent.getBooleanExtra(SHOUOLD_OPEN, DEFAULT_VALUE_DOESNT_MATTER)
and handle it accordingly
check this out -
https://stackoverflow.com/a/26486425/3339597

Related

adding on click event to button in notification

So i have an alarm app...and when the receiver gets an intent from an alarm class, it creates a notification and builds it..but i just cant seem to figure out how to add onclick event to that button..i want it to implement a function not to just get an intent
this is my receiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Context context= arg0;
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0);
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.mini)
.setContentTitle(context.getResources().getString(R.string.message_box_title))
.setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date))
.addAction(R.drawable.bell,"snooze",pendingIntent);
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();
Integer get_your_alarm_choice = arg1.getExtras().getInt("alarm_choice");
Log.e("alarm choice is",get_your_alarm_choice.toString());
}
any help would be really appreciated
I think you want to add a custom button in your notification and want to click it.
Please try below code if you need the same:
You have to use RemoteViews for this.
I have created one custom layout named notification_normal_view.xml.
In my notification_normal_view , I have one TextView i.e.txtSnooze and on click I want to open SnoozeActivity and if I click at any other part of notification I want to open MainActivity.
So in your receiver :
// Using RemoteViews to bind custom layouts into Notification
RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.notification_normal_view);
Intent snoozeIntent = new Intent(context, SnoozeActivity.class);
snoozeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);
PendingIntent pSnoozeIntent = PendingIntent.getBroadcast(context,NOTIFICATION_ID,snoozeIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(context, ExoVideoPlayer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);
PendingIntent pIntent = PendingIntent.getActivity(context, NOTIFICATION_ID,intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.txt_snooze, pSnoozeIntent);
Notification notificationBuilder = new Notification.Builder(context)
.setSound(soundUri)
.setSmallIcon(icon)
.setAutoCancel(true)
.build();
//set your view to notification
notificationBuilder.contentView = notificationView;
notificationBuilder.flags = Notification.FLAG_AUTO_CANCEL;
notificationBuilder.icon = R.mipmap.ic_launcher;
notificationBuilder.contentIntent = pIntent;
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder);

Click event not working on Heads-up notification

Nothing happens on clicking Accept or Reject button of the head-ups notification.
But when the head-up notification disappear and from clicking Accept and Reject from the notification panel is working.
Testing on Android 5.1.0.
Intent acceptIntent = new Intent(this, NotificationReceiver.class);
acceptIntent.setAction("com.android.test.Accept");
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, acceptIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent rejectIntent = new Intent(this, NotificationReceiver.class);
rejectIntent.setAction("com.android.test.Reject");
PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, rejectIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.fundu);
builder.setContentTitle("Test Notification");
builder.setContentText("Hello");
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCategory(NotificationCompat.CATEGORY_SERVICE);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.addAction(R.drawable.ic_check_icon, "Accept", acceptPendingIntent);
builder.addAction(R.drawable.ic_action_close, "Reject", rejectPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
Just setting the vibration, makes it working fine.
builder.setVibrate(new long[0]);
The key is to call setContentIntent on the Notification Builder and passing it a PendingIntent. The Full code with comments explaining each step is included below. See the part named "THIS IS THE PERTINENT PART" (The Full code is included for completeness sake.)
// Use Notification Builder to start things off
// There are other ways of acquiring a Notification Builder; this is just an example
String channelId = "channelId";
String title = "title";
String body = "body";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId);
notificationBuilder
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle(title)
.setContentText(body);
//--------------- THIS IS THE PERTINENT PART ---------------
// Prepare Intent for creating PendingIntent
Intent intent = new Intent(context, ActivityToStart.class);
// Create Pending Intent
int requestCode= 1234; // requestCode has to be a unique ID for EACH PendingIntent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
requestCode,
PendingIntent.FLAG_UPDATE_CURRENT // use to prevent re-using current Activity Intent
);
notificationBuilder.setContentIntent(pendingIntent);
// Finally, create the Notification
Notification notification = notificationBuilder.build();

set onCliccklistener to notification

I have an application that shows a play pause notification.I want to when a user tap on the notification a service in the app be called and I do my stuff in that service.
I have done it for my notification item widgets like buttons but now I want to handle user tap on whole of notification bar.any help?
This is my code for handling on items click:
Intent intent = new Intent(context , MyNotificationService.class);
intent.putExtra("ID","play");
PendingIntent pendingIntent = PendingIntent.getService(context,0,intent,0);
myRemoteView.setOnClickPendingIntent(R.id.myButton);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(this, YourActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(YourActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(resultPendingIntent);
There you go :) This will open 'YourActivity' when the notification is tapped.

How to check if Activity start from notification

I have problem my MainActivity can be created by 3 ways:
1) standart launch App
2) from Service
3) from notification click.
How I can check when it starts from notification click?
Notification code:
private void createNotification()
{
Log.d("service createNotification",MainActivity.TAG);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this,MainActivity.class);
intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(this.getString(R.string.notification_title))
.setContentText(this.getString(R.string.notification_text))
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher);
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification());
}
add
intent.putExtra("started_from","notification");
to the code that starts the intent from the notifications, and the same thing to the other startActivity calls just change the value, then inside your activity
String startedFrom = getIntent().getStringExtra("started_from");
for more refer to this question: How do I get extra data from intent on Android?

How to add tap action on notification?

I want to add a tap action (exit (finish)) to a notification.
I'm making a simple app with several classes, and I want them all finished when I tap the notification.
Here is my notification code:
mMN = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification();
n.icon = R.drawable.ic_launcher;
n.tickerText = "Tap this notification to exit";
n.when = System.currentTimeMillis();
Intent nid = new Intent(MainActivity.this, stopservice.class);
PendingIntent ci = PendingIntent.getActivity(this, 0, nid,PendingIntent.FLAG_UPDATE_CURRENT);
CharSequence ct = "TAP";
CharSequence tt = "Tap here to exit";
n.setLatestEventInfo(this,ct,tt,ci);
mMN.notify(NOTIFICATION_ID, n);
I'm making a reference to stopservice class (where it is my stop service code) on Intent nid, but I'm not quite sure if it's a correct reference.
hope that my question is clear.
You should be using a notification builder:
mMN = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent nid = new Intent(MainActivity.this, stopservice.class);
// If you were starting a service, you wouldn't using getActivity() here
PendingIntent ci = PendingIntent.getActivity(MainActivity.this, NOTIFICATION_ID, nid, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setContentTitle("TAP")
.setContentText("Tap here to exit")
.setTicker("Tap this notification to exit")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(ci)
.setAutoCancel(true); // auto cancel means the notification will remove itself when pressed
mMN.notify(NOTIFICATION_ID, builder.getNotification());
Your code is set to launch the Activity "stopservice" (which technically should be "StopService" with naming conventions), are you sure that class is an Activity?
Also, make sure your Activity is registered in your app's manifest:
<activity android:name="[package-name].stopservice" android:label="#string/app_name"/>

Categories

Resources