Receiving the last notification data on tap - android

In my application if there are 3 notification, On tapping of any notification I am getting data of last notification. I am setting number with each notification but If there are multiple notification I get the data of last notification.
Here is my code when I set notification
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = userSetMeetingDate;
calendar.set(Calendar.HOUR_OF_DAY, conferenceHour);
calendar.set(Calendar.MINUTE, conferenceMinute);
Bundle bundle = new Bundle();
bundle.putInt(KEY_CONFERENCE_ID, entity.getId());
Intent alarmIntent = new Intent(this, OnConferenceAlarmReceiver.class);
alarmIntent.putExtra(KEY_CONFERENCE_ENTITIY_BUNDLE, bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, entity.getId(), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
and
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(entity.getName())
.setContentText(entity.getConfrenceNumber())
.setContentIntent(pendingIntent)
.setVibrate(vibrate)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.addAction(android.R.drawable.ic_menu_call, "Call", pendingIntent)
//.addAction(android.R.drawable.ic_menu_edit, "Update", pendingIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int) ((long) entity.getId());
notificationManager.notify(id, notification);
Thanks in Advance

Related

Sending in App Notification daily at Specific Time

I'm developing an android app in which i want to send notification daily at 14:30. I am able to get notification on time, But my problem is that, whenever i open app after 14:30, i received notification everytime. How to solve that ?
Code to send notification is here ... !
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 30);
Intent intent = new Intent(getApplicationContext(), TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
and code of TimeAlarm.class is ....
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context, MainActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.ic_btn_speak_now)
.setContentTitle("Title")
.setContentText("Text")
.setAutoCancel(true);
manager.notify(100,builder.build());
Try this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.ic_btn_speak_now)
.setContentTitle("Title")
.setContentText("Text")
.setOngoing(false)
.setWhen(calendar.getTimeInMillis());
.setAutoCancel(true);
manager.notify(100,builder.build());
setWhen
NotificationCompat.Builder setWhen (long when)
Set the time that the event occurred. Notifications in the panel are sorted by this time.
You can even try setShowWhen
Control whether the timestamp set with setWhen is shown in the content view.

How to update broadcast receiver. (multiple notification)

Variables in BroadcastReceiver are not updating every time i set a notification/alarm manager.
"receiver (recycler)" is from a fragment.
receiver" is from a BroadcastReceiver class.
onCreateView
intentAlarmManager = new Intent(context, NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intentAlarmManager, PendingIntent.FLAG_UPDATE_CURRENT);
Notification Method
private void setNotification(int hour, int min, int interval, int uniqueID) {
//get instance of the calendar
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
//create delayed intent
pendingIntent = PendingIntent.getBroadcast(context, uniqueID, intentAlarmManager, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * (interval * 30), pendingIntent);
}
Recycler its attached to a switch listener
setNotification(Integer.parseInt(model.getHour()), Integer.parseInt(model.getMinute()), Integer.parseInt(model.getInterval()), Integer.parseInt(model.getTime()));
Receiver
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intentToStartWhenAlarmSets = new Intent(context, LoginActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intentToStartWhenAlarmSets, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Content Title")
.setContentText("Notify " + HomeFragment.notifMedName)
.setSound(notifSound)
.setVibrate(pattern)
//swipable
.setAutoCancel(true);
Log.d(ContentValues.TAG, "receiver " + HomeFragment.notifMedName);
notificationManager.notify((int) System.currentTimeMillis(), builder.build());
I figured it out, i think. Although I'm not sure if this is the best/correct way (i'ts working though).
So in in my receiver class i added
intent.getStringExtra("string);
and in my fragment i added
intentAlarmManager.putExtra("string", notifMedName);
getActivity().sendBroadcast(intentAlarmManager);
and also i changed all pending intent from context to getActivity().
Feel free to answer if you guys have a better solution.
More info here.

How to create multiple local notifications in android

In my android app I need to show multiple local notifications on a particular day at different time intervals,I used alarm manager and broadcast receiver to do for one notification but when I was trying to implement multiple notifications only the second one is been displayed.
Here is my MainActivity
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(2015, 10, 23, 15, 03);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
Here is BroadcastReceiver
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent1 = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("TRR - 2016")
.setContentText("Station - 1 closed grace period only 10min")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent1).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mCounter, notification);
Notification notification1 = builder.setContentTitle("TRR - 2016")
.setContentText("Station - 2 closed grace period only 10min")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent1).build();
NotificationManager notificationManager1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager1.notify(++mCounter, notification1);
Each Notification must have its own Notification ID.
Your problem is here:
notificationManager.notify(0, notification);
Specifically, the "0" is the ID of the Notification. If you do not provide a different ID, Android will think you are simply updating the Notification that already exists.
Documentation.
public void notify (int id, Notification notification)
Post a notification to be shown in the status bar. If a notification
with the same id has already been posted by your application and has
not yet been canceled, it will be replaced by the updated information.
You could try something like this:
private int mCounter = 0;
...
notificationManager1.notify(++mCounter, notification1);

Status bar notification auto starts activity

I am trying to do scheduled notification. All works except: When application is active and minimized. Notification auto starts activity without waiting for user to click on it.
On reveive:
public void onReceive(Context context, Intent paramIntent) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Notification notification = new Notification(R.drawable.logo_f, context.getResources().getString(R.string.notification_text), System.currentTimeMillis());
Intent notificationIntent = new Intent(context, TimeLeftActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, context.getResources().getString(R.string.notification_text), "", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound=alarmSound;
// Fire the notification
notificationManager.notify(1, notification);
}
My notification start method:
private void createScheduledNotification(int sec)
{
// Get new calendar object and set the date to now
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Add defined amount of days to the date
calendar.add(Calendar.SECOND, sec);
// Retrieve alarm manager from the system
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// Every scheduled intent needs a different ID, else it is just executed once
int id = 1;
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, TimeAlarm.class);
// Prepare the pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
// Register the alert in the system. You have the option to define if the device has to wake up on the alert or not
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
EDIT after Kirill answer. Error still persist. Notification auto starts pending intent and does not wait for click.
#Override
public void onReceive(Context context, Intent paramIntent) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Intent notificationIntent = new Intent(context, TimeLeftActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(context.getResources().getString(R.string.notification_text))
.setContentIntent(intent)
.setSound(alarmSound)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Fire the notification
notificationManager.notify(1, notification);
}
It is hard to find error, because you use deprecated API in your code, you should to use Notication.Builder
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
If you need to support old versions you can use NotificationCompat
UPDATE
This is sample from my app, it throws a notification, which open activity by click, I marked method to add intent.
String message = context.getString(R.string.notif_message);
Intent notificationIntent = new Intent(AddBpRecordActivity.ADD_ACTION);
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notif_logo)
.setContentTitle(message)
.setContentText(billet.comment)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
>>> .setContentIntent(PendingIntent.getActivity(context, (int) billet.id, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setWhen(System.currentTimeMillis());
Notification notification = nb.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) billet.id, notification);

Generating Alarm with notification issue in android

Here is my code.
From where i am not able to generate alarm programmatically..
Calendar cal = Calendar.getInstance();
int id = (int) cal.getTimeInMillis();
Intent myIntent = new Intent(this,MyScheduledReceiver.class);
myIntent.putExtra("taskTitle", taskTitle.getText().toString());
myIntent.putExtra("taskDetails", taskDetails.getText().toString());
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(year, mon, day,tp.getCurrentHour(),tp.getCurrentMinute());
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), id,
myIntent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
I declared in the manifest file
<receiver android:name=".MyScheduledReceiver"></receiver>
An in the broadcast reciever.
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, MyScheduledReceiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("taskTitle");
String note=extras.getString("taskDetails");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
manger.notify(NOTIFICATION_ID++, notification);
I have something similar and it works.
First hi declare a Pending:
Intent intent = new Intent(Global.a, EventAlarmReceiver.class);
intent.putExtra("title", ""+cd.title);
intent.putExtra("desc", ""+cd.description);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Global.a.getApplicationContext(), (int) (cd.alarmTime), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, cd.alarmTime, pendingIntent);
and in EventAlarmReceiver class i have:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
String text = String.valueOf(intent1.getCharSequenceExtra("title"));
Notification notification = new Notification(R.id.icon,
text, System.getTimeInMillis());
notification.vibrate = new long[]{100,250,300,330,390,420,500};
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, ShowEventActivity.class);
intent.putExtra("title", String.valueOf(intent1.getCharSequenceExtra("title")));
intent.putExtra("desc", String.valueOf(intent1.getCharSequenceExtra("desc")));
String text1 = String.valueOf(intent1.getCharSequenceExtra("desc"));
PendingIntent activity = PendingIntent.getActivity(context, (int) System.getTimeInMillis() , intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, text, text1, activity);
notificationManager.notify((int)System.getTimeInMillis(), notification);
make sure that in the Manifest you declare declare the package where the Receiver is: for example in my case the EventAlarmReceiver class is the package com.app.name.notifications so in the manifest i have:
<receiver android:name=".notifications.EventAlarmReceiver"></receiver>

Categories

Resources