Notification comes again when app starts - android

As a beginner, I wrote a code for notification. I put that code in MainActivity.
Now the problem is, my app shows notification at sharp 8 AM. But During other time, if i restart my app, it shows notification again(at any other time). Basically I am not cancelling it notification as it appears. Though I have put AutoCancel true. Am i missing something?
Code in MainActivity, onCreateMethod:
calendar = Calendar.getInstance();
PendingIntent pendingIntent ;
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(MainActivity.this, NotificationBroadcasrReceiver.class);
intent.putExtra("NotificationType","GoodMorning");
pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), (int)System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getBaseContext()
.getSystemService(getBaseContext().ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
After this it comes to BroadCast Receiver :
the code inside onReceive is
Intent intentNew = new Intent(context, CustomIntentService.class);
intentNew.putExtra("NotificationType",intent.getStringExtra("NotificationType"));
context.startService(intentNew);
Now custom intent servcice class has following code..
Intent notificationIntent = new Intent(this,CustomActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.happiness_icon);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContentTitle("Positive Minds");
mBuilder.setCustomBigContentView(remoteViews);
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManager mNotificationManager = (NotificationManager)this
.getSystemService(this.NOTIFICATION_SERVICE);
mNotificationManager.notify((int)System.currentTimeMillis(), mBuilder.build());

I used below mentioned code to fix this.
if(calendar1.getTime().compareTo(new Date()) < 0)
calendar1.add(Calendar.DAY_OF_MONTH, 1);

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.

Notification wtih AlarmManager in fragment

Have a trouble trying to get an alarm manager to work with a notification inside. This is to give the user of the app, a notification on a certain point.
My alarm is setup inside a fragment, with the code as following.
Intent notificationIntent = new Intent(getContext(), DeviceBootReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int hour = Integer.parseInt(vars.getShowRapport().getFinishTime().split(":")[0]);
int min = Integer.parseInt(vars.getShowRapport().getFinishTime().split(":")[1]);
vars.getShowRapport().getFinishDate().setHours(hour);
vars.getShowRapport().getFinishDate().setMinutes(min);
long futureInMillis = System.currentTimeMillis()+1000*60;
//long futureInMillis = vars.getShowRapport().getFinishDate().getTime();
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
getFragmentManager().beginTransaction().replace(R.id.makeRapportFragment,fragment).addToBackStack(null).commit();
In my manifest i have
<receiver android:process=":remote" android:name=".DeviceBootReceiver">/receiver>
And finally in my receiver called DeviceBootReceiver i have.
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ejservicebookincon)
.setContentTitle("Rapport finishing")
.setContentText("Rapport on item should be done.");
Intent notificationIntent = new Intent(context, ServiceRapportMain.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
Don't know where my problem is, and my receiver never seems to be activated, so can't check if that part work as of yet. Would like help if someone could point out, where my problem is. And just as a heads up i'm working in Android Studio.
As i wrote in the comment above, the error i had was that i used AlarmManager.ELAPSED_REALTIME_WAKEUP where then one i should have used here was AlarmManager.RTC_WAKEUP

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.

Notification is triggered again and again

I am creating a notification on a activity called Mainmenu using alarm here is code
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent,0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
On notification received , on click of the notification i have to open the same Mainmenu activity.
Problem: again one more notification is generated , again click on notification again Mainmenu activity will open and continues
Here is Notification code
NotificationManager mManager = (NotificationManager) getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("XXXXXXXX")
.setContentText("Please sync data.").setAutoCancel(true);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 1, intent1, 0);
notification.setContentIntent(pendingNotificationIntent);
Log.d("On service", "Alarms set for everyday 2 pm.");
mManager.notify(0, notification.build());
return Service.START_NOT_STICKY;
You should check the application has started from push notification or normal.
How to do this.
Notification generate code
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
intent1.putExtra("isFromNotification", true);
onCreate() of MAFLogonActivity.java
boolean isFromNotification = getIntent().getBooleanExtra("isFromNotification", false);
if(!isFromNotification){
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent, 0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
}
Hope this would help you.

Clicking notification without starting activity Android

I'm new to android programming and I just learned how to make a notification via an Intent and a PendingIntent. I wondered if I could make this notification clickable (link to my MainActivity) without starting a new Activity.
This is what I have right now (AlarmReceiver excluded) as a function in my MainActivity:
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
I reckoned I should change "this" to something else, but I wouldn't know how to do this. Any help would be appreciated!
You can use the notification Builder, in my case the compat builder.
NotificationCompat.Builder
Setting the content, I've got mine from the params. You have to replace them with your respected once.
builder.setContentTitle(params.getTitle())
.setContentText(params.getText())
.setWhen(params.getTime())
.setTicker(params.getTickerText())
.setPriority(params.getPriority())
.setOngoing(params.isOngoing())
.setOnlyAlertOnce(true)
.setAutoCancel(params.getAutoCancel())
.setStyle(params.getStyle());
Creating PendingIntent and also adding it to the Notification through the builder. This intent should be pointing to your Launcher Activity and from there you can handle it in OnCreate.
builder.setContentIntent(pendingIntent);
Then push the notification
mNotificationManager.notify(id, notification);
You can also make these notifications pointing to a BroadCastReceiver and handle a click on the notification from there, on its onReceive method.
Intent alarmIntent = new Intent(this, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(this, 0,
alarmIntent , PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); ...try this

Categories

Resources