Android: activity referesh when noification fire - android

Currrently I am working on Notification in andorid.
Here code is notification arrive....
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setTicker("New Task Here!!!");
if(taskdata.get(0).getDescription().equals(""))
{
int stringId = context.getApplicationInfo().labelRes;
String appname = context.getString(stringId);
mBuilder.setContentTitle(appname);
}
else
{
mBuilder.setContentTitle(taskdata.get(0).getDescription());
}
long l = Long.parseLong(taskdata.get(0).getDateTime());
Date date =new Date(l);
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a ");
String alarmtime = formatter.format(date);
String dateString= DateFormat.format("dd/MM/yyyy",date).toString();
mBuilder.setContentText(alarmtime+","+dateString);
Intent resultIntent = new Intent(context, TaskDetail.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
Bundle bundledata = new Bundle();
bundledata.putString("ImagePath", taskdata.get(0).getImagePath());
bundledata.putString("Des", taskdata.get(0).getDescription());
bundledata.putString("DateTime", FlagValue);
resultIntent.putExtras(bundledata);
stackBuilder.addParentStack(TaskDetail.class);
//ringtone on
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
/* Increase notification number every time a new notification arrives */
mBuilder.setNumber(++numMessages);
mBuilder.setAutoCancel(true);
// Adds the Intent that starts the Ac tivity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
// here this activity will refresh when fire the receive
Intent activityintent = new Intent(context,MainActivity.class);
PendingIntent contentintent = PendingIntent.getActivity(context, 0, activityintent, PendingIntent.FLAG_UPDATE_CURRENT);
activityintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activityintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());
}
My issue is :
When Nofification come that time MyActivity.class Activity will be refresh.
I am also refer this link Android Refresh Activity from Notification but in my application not wok.
If you have Any Idea Please Help me.
Thank you in Advance.

Add the following function in the end of the notification function .
onCreate(null)

Related

Notification do nothing when clicked

So I would like to create a notification that, when clicked, will open my MainActivity and do something according to the extras I've put in the intent.
Right now, the notification works perfectly, but once I click it, nothing happens. I've search a little bit and have done some modification to my code :
I have added android:launchMode="singleTop" to my activity in the manifest.
I have put the code to execute in my MainActivity in the onNewIntent method :
#Override
protected void onNewIntent(Intent intent){
System.out.println("Entering onNewIntent!!!!!!!");
if(getIntent().getExtras() != null) {
url = getIntent().getExtras().getString("url");
System.out.println("URL in Main Activity : " + url);
if (url != null && url != "") {
saveFile(url);
dezipFile();
}
}
}
But nothing changed, I've put some System.out to see if it even reach the beginning of the onNewIntent... But nothing is shown on the console.
Here is the code where I create the notification :
private void createNotification () {
Intent downloadIntent = new Intent(getApplicationContext(), MainActivity.class);
downloadIntent.putExtra("url", url);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent downloadPendingIntent = PendingIntent.getService(getApplicationContext(), 0, downloadIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Notif")
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle(getApplicationContext().getString(R.string.notif_title))
.setContentText(getApplicationContext().getString(R.string.notif_content))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(downloadPendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notification_download", "Dowload BD infos", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
builder.setChannelId("notification_download");
}
NotificationManagerCompat notification = NotificationManagerCompat.from(getApplicationContext());
notification.notify(1, builder.build());
}
If anybody knows what I did wrong, it will be really appreciated!
Add this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "Notif")
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle(getApplicationContext().getString(R.string.notif_title))
.setContentText(getApplicationContext().getString(R.string.notif_content))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(downloadPendingIntent)
.setAutoCancel(true);
Intent mIntent = new Intent(context, YourActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(mIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
You can set the activity launched when user clicks on a notification as follows:
Intent resultIntent = new Intent(this, targetActivityClass);
Intent backIntent = new Intent(this, backActivityClass);
Intent[] intentArray = new Intent[]{backIntent, resultIntent};
PendingIntent resultPendingIntent = PendingIntent.getActivities(this, 0, intentArray,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
builder.setContentIntent(resultPendingIntent);
This code also sets the activity launched when user clicks the back button after clicking the notification.

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);

How to make notification clickable?

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.

Android - notification putExtra issue

I've searched for about hour to find some solution how to send extras to activity when user clicks in notification box but everything I've found didn't work for me.
I need to pass event ID to activity where I show user info about this event.
I've used setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), setAction("Action"), PendingIntent.FLAG_UPDATE_CURRENT and combination of all of these solution but neither didn't work.
That's my code:
Notification.Builder notiBuilder = new Notification.Builder(context);
notiBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.logo));
notiBuilder.setSmallIcon(R.drawable.logo);
notiBuilder.setContentTitle(context.getString(R.string.eventNitificationTitle));
notiBuilder.setContentText(obj.getString("nazwa") + " " + obj.getString("data"));
Intent eventIntenet = new Intent(context, EventActivity.class);
eventIntenet.setAction("Action_"+id);
eventIntenet.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
eventIntenet.putExtra("eventID", id);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, eventIntenet, PendingIntent.FLAG_UPDATE_CURRENT);
eventIntenet = null;
notiBuilder.setContentIntent(pIntent);
pIntent = null;
NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notiManager.notify(Integer.parseInt(id), notiBuilder.build());
} else {
notiManager.notify(Integer.parseInt(id), notiBuilder.getNotification());
}
And way to get my Int:
this.eventID = getIntent().getIntExtra("eventID", -1);
Everytime when I click in notification, I'm moving to activity but getExtra returns -1.
You can try something along these lines:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ChatService.this).setSmallIcon(R.drawable.app_icon)
.setContentTitle(senderName)
.setContentText("Notification Text Here");
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(MyService.this, TargetActivity.class);
resultIntent.putExtra("eventID", id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyService.this);
stackBuilder.addParentStack(TargetActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());
If your app is already running and your EventActivity is already on the top of the stack, this code:
this.eventID = getIntent().getIntExtra("eventID", -1);
will always use the Intent that EventActivity was started with the first time.
You should override onNewIntent() and get the "extra" from the Intent that is passed as an argument to that method.

Android AlarmManager putExtra to PendingIntent and NotificationCompat.Builder

I'm trying to putExtra to Activity which will be launched after clicking on notification, but instead of value that I set I'm getting default value. This is my code in AlarmReceiver:
Intent notifActiv = new Intent(context, NotificationActivity.class);
notifActiv.putExtra("ID", id);
PendingIntent pI = PendingIntent.getActivity(context, 0, notifActiv, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(string).setTicker("You got meeting today!")
.setContentText("Click here for more details");
mBuilder.setContentIntent(pI);
mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
And this is my NotificationActivity where I'm trying to get Extras:
Intent intent = getIntent();
int id = intent.getIntExtra("ID", 0);
Could you please tell my where I'm doing something wrong?

Categories

Resources