set onCliccklistener to notification - android

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.

Related

The other notifications is lost the click behaviour, when a notification is chosen at device's system tray in android

If there're two my app's notifications at device's system tray, the user choose one notification, the app's opening but all of other notifications is lost the click behavior, it means user cannot click them anymore. How can I prevent this action?
My code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(getString(R.string.test))
.setContentText(messageBody);
Intent intent = new Intent(this, NotifActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString(NotificationViewActivity.MESSAGE_EXTRA, messageBody);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(50) + 1, builder.build());
int mId = (int) System.currentTimeMillis();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.imagename)
.setContentTitle("Title")
.setContentText("message")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, activityname.class);
//activityname is the name of activity which you want to launch
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
I detected my root cause in below code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Because the request code is the same for all notifications, so when we click to the one notification, the other lost their click listener. Fix like below codes
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_ONE_SHOT);

stackBuilder.addParentStack is Not Working- Navigate to Main Activity (OnBackPressed)

When user Click on Notification, it will open New Activity That's Working fine. But When i Presss Back button it closed Application
What i want ?
When i click on Back button it return MainActivity(Selected Activity )Everytim.
private void generateNotificationNew(Context context, String message,String pushIdmessage) {
Intent resultIntent = null;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(message);
resultIntent = new Intent(context,ResultActivity.class);
//Intent resultIntent = new Intent(this, AvailableJobActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
5,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
stackBuilder.addParentStack or stackBuilder.addNextIntent may not working properly.
Any Alternative option Thank you.
For generate notification you need to put following code...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Notificatin Title")
.setContentText("message");
Intent notificationIntent = new Intent(this , ResultActivity);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( 0 , builder.build());
And for the handle back event you need to check following things in onBackPressed() in Result Activity
#Override
public void onBackPressed() {
if(this.isTaskRoot())
startActivity(new Intent(this , MainActivity.class));
super.onBackPressed();
}
Now you can redirect to MainActivity from notification started activity..
Enjoy it...:-)

Opening application from notification

Let's say that we have Activity which displays funny picture and name it FunnyActivity. This Activity can be launched from MainActivity, which is base Activity in out application, after clicking button. We want also to push some notifications sometimes and when user clicks on notification this FunnyActivity should be launched. So we add this part of code:
Intent notificationIntent = new Intent(this, FunnyActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), notificationIntent, 0);
and this PendingIntent is using in notification builder
setContentIntent(intent)
Of course FunnyActivity is beautifully launching, but we want to open MainActivity when user clicks back button on FunnyActivity.
How can we achieve that? Please remember that when user came back to MainActivity he can open FunnyActivity again from button.
Try this:
// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailsActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
TaskStackBuilder.create(this)
// add all of DetailsActivity's parents to the stack,
// followed by DetailsActivity itself
.addNextIntentWithParentStack(upIntent)
.addNextIntent(detailsIntent);
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
Source: Create back stack when starting the activity
You can try this:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
Intent resultIntent = new Intent(this, FunnyActivity.class);
// Adds the back stack
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
Intent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)/*.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))*/
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(msg)
.setContentIntent(resultPendingIntent)
.setAutoCancel(true);
This is how you can achieve solution to your problem. Hope this helps you

Heads-up Ongoing Notification not hide

I need heads-up notification which will be not cancelable from notification drawer so I created Heads-up Ongoing notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo_notification)
.setContentTitle("Title")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setGroup(KEY)
.setGroupSummary(true)
.setOngoing(true)
.setColor(context.getResources().getColor(R.color.tab_indicator_color));
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, Activity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(WActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent pIntent = new Intent(context, PService.class);
pIntent.setAction("ACTION");
PendingIntent piPause = PendingIntent.getService(context, NOTIFICATION_ID, pIntent, 0);
mBuilder.addAction(icon, title, piPause);
Intent sIntent = new Intent(context, SService.class);
sIntent.setAction("ACTION");
PendingIntent piDismiss = PendingIntent.getService(context, NOTIFICATION_ID, sIntent, 0);
mBuilder.addAction(icon2, title2, piDismiss);
The problem is that After notification shows on the top of screen it does not hide to notification drawer. But if notification is not ongoing, it hides. I need heads-up notification which will hide to notification drawer.
AFAIK, a notification needs the following properties to be set to be shown as a heads-up notification.
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
setOngoing() means that the Notification is ongoing and cannot be hidden by the user. I believe that is probably why your Notification stays on the top and doesn't hide. Remove setOngoing(true) and it should work.

custom notification button click

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

Categories

Resources