how to show notification on device start up - android

I am developing notification app in android that shows notification on particular date and time that i set using date picker. It shows correctly if my app is running in background. But when i force stop my app or if device switched off &restarted, notification does not show. How to show notification even if app force closed or device restarted.

You can't. Notifications are attached to application's context. If the application is killed/destroyed, your notification also goes away with it.
What you may do is to re-create those notifications once your application or it's service is started. For that, make sure you do catch android.intent.action.BOOT_COMPLETED broadcast in order to implement this automatically.

You should consider using AlarmManager instead of a service.
Set an alarm at the desired date/time with a custom intent. In the BroadcastReceiver, you create and show the Notification.
If the Device is restarted, you might need also to listen to the BOOT_COMPLETED Intent and reset the alarms.
EDIT:
An example:
long time = // time in milliseconds of when you want your Alarm
PendingIntent mIntent = PendingIntent.getBroadcast(context,
0, new Intent("YOUR_CUSTOM_INTENT"), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
time, mIntent );
Then Catch the YOUR_CUSTOM_INTENT intent in a BroadcastReceiver, show the notification and set the following alarm.

Related

Registering an alarm in Android, where does it go?

I have an Android application which provides users in a particular city info on food and drink deals. I would like to create an alarm so that a user can click "Remind Me" and when a particular deal begins (such as 2pm) the app opens to that specific info.
Here is the code that creates my alarm:
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ViewListingActivity.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
alarmMgr.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
Question:
Where in my phone can I see this alarm registered? I looked in the Clock -> Alarm but I do not see it.
I just want to see it so I know it has been setup correctly.
Where in my phone can I see this alarm registered? I looked in the Clock -> Alarm but I do not see it.
I think you've got a wrong idea about the concept of AlarmManager :
AlaramManager is an android system component allowing you (as a developer) to schedule execution of one of the three:
1) start service
2) send broadcast
3) start activity.
one of the best thing is that your app don't have to be running at all. instead, the the OS will wake up your process if your application scheduled with the AlaramManager any alarm for this time, and start the service/activity/broadcast based on when you specified..
Registering an alarm in Android, where does it go?
the pendingIntent object you provided to the alramManager is saved by the system, in order to execute it at the right time.
there is no any system UI component that shows you that visually!! and there is not make sense that there will be such, because - app can schedule alarms for any kind of reasons that most of them not directly been visualized to the user.
for example: my app schedule alram each two hours, in order to update in front of my company servers, the local database of my app. this action don't involved any interaction from the user, and happens completely in background.
after understanding that, I can tell you that there is way to check programatically what scheduled alarms your app have in front of the AlaramManager: follow this answer: How to check if AlarmManager already has an alarm set?
for more information, I strongly recommends you to read the documention of PendingIntent , and AlaramManager:
http://developer.android.com/reference/android/app/AlarmManager.html
http://developer.android.com/reference/android/app/PendingIntent.html
You can do it by using ADB. From the ADB console within Android Studio try the following:
adb shell dumpsys alarm
This should show you all of the alarms that have been set, as well as their details, such as alarm time, interval, etc.

Is there an equivalent solution like iOS's localNotification on Android platform?

My app needs to show notifications to users at certain specified time in the future (maybe months away). In iOS, all I need to do is this:
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
However, in Android, it is much more complicated than I originally expected:
I have to install alarms with Alarm Manager, to start a service at each specified time in future.
These services, in turn, create a notification, and use Notification Manager to push the notification to status bar, etc.
But this solution has problems:
If the device is restarted before the alarm fires, the alarm is lost. I can register for boot-up broadcast like this and re-install the services. However, I really want to avoid installing duplicate alarms for the same event, but it seems there is no way to get currently installed alarms from the Alarm Manager?
The notification is composed at the scheduled time in future (say a month later), not when I set the notification(now). At that time, the required data for the notification may no longer be available. I don't see any way around this, except to store the relevant data, and wait for the alarm to fire.
Is there a pain-free solution to the local notification problem on Android?
If the device is restarted before the alarm fires, the alarm is lost.
I can register for boot-up broadcast like this and re-install the
services. However, I really want to avoid installing duplicate alarms
for the same event, but it seems there is no way to get currently
installed alarms from the Alarm Manager?
How are you going to get duplicated alarms when the device is restarted? When the device is restarted all alarms are canceled so you would do like you said where you start it again in the BroadcastReceiver when the device boots
The notification is composed at the scheduled time in future (say a
month later), not when I set the notification(now). At that time, the
required data for the notification may no longer be available. I don't
see any way around this, except to store the relevant data, and wait
for the alarm to fire.
that is correct you need to store the data that you want to show in the notification, SharedPreferences will probably be the easiest
EDIT:
You do not need to keep a reference to the pending intent all you have to do is create the intent the same way example
when you first created the alarm you used this intent
Intent intent = new Intent(context,MyClass.class);
//any flags or extras
PendingIntent.getBroadcast(context, 0, intent, 0);
Now you want to cancel that alarm so just create the same intent again, it must be a 1 for 1 of the origional
Intent intent = new Intent(context,MyClass.class);
//any flags or extras that you previously had
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.cancel(pi);

Android: Calendar integration with custom notifications

I need to integrate, in my android application, a calendar from which i can set new event.
I know that is possible call the android calendar (using intent), but what I need is slightly different, for this reason:
I need that when the time of the event is reached, some code is started, and no default notification occurs! In other words I want create a custom notification when alarm of event goes off. Maybe I need to use AlarmManager
How can I solve this problem?
If your problem is that you need to launch some kind of intent at a specified time then you can use AlarmManager. It allows you to schedule your application to be run at some point. When an alarm goes off, the Intent that had been registered for it is broadcast by the system.
For More information about AlarmManager, you can check this link : AlarmManager

AlarmManager object after turning off and on the phone

In my app, I set an alarm
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
...
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
It works fine unless I turn off and turn on the phone.
To be more specific, let's say at 10:20, I set an alarm to 10:22 and I turn off and turn on the phone at 10:21, alarm won't work.
What might be the problem? Is that a broadcast issue of the pendingIntent there or should I set some flags of the alarmManager object for it to work in such conditions?
The documentation about the AlarmManager says that :
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
It seems that the AlarmClock included by default by Android does work even after a reboot.
On way to keep your alarms working after a reboot, is to start your application on boot completed and set up all the alams again with the AlarmManager.
(In fact you may want to just setup your alarms using a Broadcast, not start your app)
Here is a StackOverflow question dealing about lunching an app on startup.
You wan also check out how the default AlarmClock does this by reading from the source.
You can read and download it from here

How to set multiple alarms in android

I wanted to create a birthday reminder app. I save the name of person , date of birth and a birthday message in the database . I wanted to send the message automatically on that date .
Can anyone suggest an idea to do this. Can shared preference be used here? Can the specific id be passed to the database on a specific date for sending greetings to a person at his DOB.
Can anyone please suggest an idea to handle multiple alarms in this case.
Ignoring the fact that there are multiple apps that can do this already (and as a disclaimer I've released an app which does exactly that) here's how I did it.
Simple Steps:
Set an alarm to wake the device once a day
When you receive an alarm grab a wake-lock
Work out if any birthdays are going to happen today
If so send / queue a message etc
Set the alarm to be tomorrow
You will also need to catch the reboot of the device because any alarms will be destroyed when the device reboots. In which case simply have the intent for BOOT_COMPLETED call into the steps above at number 2.
My alarm code:
long alarmTime = System.currentTimeMillis()+(24*60*60*1000);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
Intent intent = new Intent("<package name>.WAKEUP_ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
I actually specify a fixed point in time each day, say about 5am to wake the device and work out what might need to be done that day, but that's relatively easy to deal with (and happens elsewhere in my app for other reasons).
Whilst you could set alarms for all the birthdays over the next year it's a waste of time as all alarms are removed when the device reboots and if the user changes anything you may have to throw the alarm away anyway.
If you really want to pass a database ID through the alarm simply add it to the Intent:
Intent intent = new Intent("<package name>.WAKEUP_ALARM");
intent.putExtra("DatabaseKey", 1);
Sending the message (assuming an SMS message?) automatically requires that you have the SEND_SMS permission and you send an SMS message in the background - like this stackoverflow answer

Categories

Resources