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);
Related
My notification can show but I would also want it to be clickable so that when it's clicked it would open the same activity it came from.
public void acceptNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(RequestConfirm.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("PEOPLE HELPER");
builder.setContentText("Your request has been accepted");
Intent intent = new Intent(RequestConfirm.this, BroadcastFragment.class); //creates an explicit intent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(RequestConfirm.this);
stackBuilder.addParentStack(RequestConfirm.this); //adds the intent
stackBuilder.addNextIntent(intent); //put the intent to the top of the stack
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); //(id, flag) //creates a pending intent
builder.setContentIntent(pendingIntent); //adds the PendingIntent to the builder
NotificationManager notificationManager = (NotificationManager) RequestConfirm.this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
like this :
Intent gotoIntent = new Intent();
gotoIntent.setClassName(getApplicationContext(), "FULL CLASS NAME");
PendingIntent contentIntent = null
contentIntent = PendingIntent.getActivity(getApplicationContext(),
(int) (Math.random() * 100), gotoIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Now set in notification Builder:
.setContentIntent(contentIntent)
To start the activity, you have to use the flag Intent.FLAG_ACTIVITY_NEW_TASK:
Intent intent = new Intent(this, YourActivityClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Call notificationManager.notify(0, builder.build()); when you want to show the notification. By clicking that pending intent will be started.
try this
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(msg);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setVibrate(vibrationPattern);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
Try this , hope this will helpful for you.
NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(CurrentActivity.this);
Notification notify;
PendingIntent pending = PendingIntent.getActivity(CurrentActivity.this, 0,
new Intent(CurrentActivity.this, NextActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
notify = builder.setContentIntent(pending)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message1))
.setSmallIcon(R.drawable.logop).setTicker(excep).setWhen(System.currentTimeMillis())
.setAutoCancel(true).setContentTitle("your Notification title")
.setContentText(message1).build();
CurrentActivity refers to Activty/class that belong to your service Activity/class and nextActivity refers to Activity/class where you want to move.
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...:-)
edit: My code worked fine, I was simply giving it the wrong Activity class (multiple APK build project using shared library).
I'm using the following code to display a foreground notification for my Service. For some reason though, it doesn't show me my activity when I click on the notification.
I've checked out various StackOverflow posts and followed accordingly, and even the sample FakePlayer from CommonsWare but to no avail.
protected void updateNotification(String subtitle) {
// The PendingIntent to launch our activity if the user selects this notification
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Show notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_headset_mic)
.setWhen(System.currentTimeMillis())
.setContentTitle("AirWaves: " + G.SETTINGS.deviceName)
.setContentText(subtitle)
.setContentIntent(pendingIntent)
.setOngoing(true);
startForeground(NOTIFICATION_ID, builder.build());
}
Anyone have an idea where I might be going wrong with this?
i have also implement notification consider the following example
public void sendNotification(Context context,String message, String action) {
try {
int icon = R.drawable.notilogoboss;
String title = context.getString(R.string.app_name);
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("message", message);
intent.putExtra("action", action);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logoboss))
.setSmallIcon(icon)
.setContentTitle(title)
.setWhen(System.currentTimeMillis())
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int notId = UUID.randomUUID().hashCode();
notificationManager.notify(notId, notificationBuilder.build());
} catch (Exception e) {
}
}
Please try the following method to create the pending intent:
Intent intent = new Intent( context, MainActivity.class );
intent.putExtra( "message", message );
intent.putExtra("action", action);
TaskStackBuilder stackBuilder = TaskStackBuilder.create( context );
stackBuilder.addParentStack( MainActivity.class );
stackBuilder.addNextIntent( intent );
PendingIntent pendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );
notificationBuilder.setContentIntent( pendingIntent );
Try changing the flag for the pending intent to this:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Also, what is the startForeground(NOTIFICATION_ID, builder.build()); method that you are using? can you post that as well for additional help?
I have a notification system in my android app, when the notification time has arrived my notification shown in the notification bar and when user clicked on it, my app opens, but still the notification shown in notification bar. How can hide notification from notification bar when user clicked on it?
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
Bundle bund = intent.getExtras();
String text = bund.getString("name", "");
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle( text)
.setContentText("text");
Toast.makeText(context,"ok", Toast.LENGTH_LONG).show();
Intent resultIntent = new Intent(context, Main.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(Main.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());
// }
}
}
Do you try?
mBuilder.setAutoCancel(true);
Below is my block of code which should open NotificationActivity when the notification is tapped on. But its not working.
private void setNotification(String notificationMessage) {
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.logo)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
try this:
private void setNotification(String notificationMessage) {
//**add this line**
int requestID = (int) System.currentTimeMillis();
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);
//**add this line**
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//**edit this line to put requestID as requestCode**
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.logo)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
You might be using a notification id equal to 0. There is known issue with notification id being 0. If you will use any other id, it should work.
I added task builder and the below block code worked for me
Intent intent = new Intent(context, HomeActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Try checking your AndroidManifest.xml, double check if you are navigating to an activity with an intent-filter main action, launcher category
For example,
I couldn't make my pending intent to navigate to HomeActivity but it works when navigating to SplashScreen
Hope this helps.
If the app is already running then you need to handle it in onNewIntent
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//Handle intent here...
}
Added the requestid, but the intent was still not opening.
This is the solution that worked for me:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Setting those flags to clear the activities below the intent activity.