How to notify a user with SMS or notification when needed - android

So in my app I'm looking to have the students class schedules and the times which they need to be in class.
When the app is turned off I still want the notification to pop up that their class is about to start (possibly vibrate).
This sort of functionality is very akin to an incoming text message or notification of something like an email. I was wondering how to implement that into an app?

You need a Service which can run when the app is not opened. You should also think about a BroadcastReceiver that listens to BOOT_COMPLETED

You will need to use the AlarmManager to set an alarm when you require the notification, possibly with the RTC_WAKEUP flag so the device will wake from a sleep. From your alarm receiver you will need to take a wake lock (if you used RTC_WAKEUP) and start a service that will use the NotificationManager to display a message to the user (very similar to the incoming SMS message).
As #WarrenFaith pointed out you will need to create a BOOT_COMPLETED receiver to re-establish the alarm after the phone is rebooted as they are not persistent.

Related

Android: How to start activity when phone is sleeping, similar to Viber incomming call activity

I am developing an app and this app needs to give a clear indication to the user when some event happens.
Only thing I could do until now is giving a notification in the notification area. But, I need to give a more visible notification, similar to the behavior when phone is ringing in an incoming call.
As I can understand, the reason why android is only allowing apps to give a notification is to prevent apps from disturbing the user. But, this app I am developing plays a vital role in the job of the user, so I don't think it is inappropriate to give a such strong notification.
I know it should be doable since apps like Viber can start an activity similar to a incoming phone call, even when the device is sleeping.
Does anyone know how to get this done?
Register a broadcast receiver, and add a custom action to it say CustomAction.Instead of showing notification, throw a broadcast and add CustomAction via intent filter.
Now in the onReceive method of broadcast listener, check
if(intent.getAction.equals("CustomAction"))Intent i = new Intent(context, YourActivity);
context.startActivity(i);
Sorry for not a formatted answer, I'm driving, will update it later for more clarification.
Update
Register broadcast receiver in a sticky service. So that service can be started automatically if killed and register broadcast register again.
Don't forget to unregister broadcast receiver in onDestroy() method of service and also in YourActivity when you purpose is resolved.
Just adding a sticky service (which does nothing) fixed the issue. Adding the service prevented the process getting killed when user exits the app and removes it from recent app list.
Because of the service, the app process is running even when a no UI is visible. In this state, if an activity is shown from the GCM service, it gets shown.
You can trigger a broascast as Vinay mentioned. If it still does not work, try using wake-locks. These wake-locks help in waking the device when it is in sleep mode. It will act like force wake and after calling wake-locks, you can perform your actions.
Hope it helped..
Thanks.

android - how to push notification (reminder) on a specific time?

I checked almost all questions in stack overflow and also in google documentation and still couldn't understand what to use exactly for my cases.
What i want is, user will select reminder date in the app and in that time app will send a notification even app is closed or phone restarted between remind set time and remind time.
So what do i need to use? Which classes do i need? Broadcast Receiver, AlarmManager these two is enough or what? a sample of code 20-30 lines would be nice =)
You will need both a few things.
Setup:
You need to set your app to listen for the phone starting by registering for this intent in your manifest.
Basically you when your user selects the time, you will need to:
Save that time in a file/db/shared prefs.
Set the alarm manager to wake up your app at the right time
When the alarm manager sends
you a broadcast, start a service to do the necessary work (broadcast
receivers have to complete their work in a short amount of time, so
it's better to start a service)
In your service, remove the time
from your file/db/shared prefs
If your app ever gets the broadcast that the phone was rebooted, then all of your alarms are lost. You need to reset them using the times you saved in the file/db/shared prefs.
Check Android Developers website for calender intents so you can use them to determine the time etc.
On your broadcast receiver pass the details or timing to the broadcast receiver. Once you do that use the alarm manager etc to check if the time is perfect and simply send a push notification when the time is correct.
I say this assuming that you know already about the calendar, alarm manager, and broadcast receivers intents. If you don't I recommend googling the following with Android Developers appended to it.
Use AlertManager.set to send notification on particular time.
For re-setting Alarm Notification, use
<receiver android:name=".AfterBootReceiver" android:label="AfterBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
and
#Override public void onReceive(Context context,Intent intent){
...
}
to reset AlarmManager.set after device re-booted.

Android: Sending sms by alarm

in my application user can set an alarm and my application is supposed to send an sms if the user doesn't respond the alarm in time ( let's say alarm goes off at 10:00 pm and user didn't reach the phone to turn it off till 10:15 than the phone will send an sms). I have looked the alarm manager but couldn't figure out how to do that.
you can achieve this by using broadcast receiver. when your user set the alarm on the activity, you can trigger it to send broadcast to the broadcast receiver. you can either execute your task on the broadcast receiver or you can start a service. for reference, check the links below
http://javatechig.com/android/repeat-alarm-example-in-android
https://blog.nraboy.com/2014/10/use-broadcast-receiver-background-services-android/
Additionally, make sure that you're app auto start after bootup to make sure that the alarm is not cleared when the phone turns off.
For reference, http://karanbalkar.com/2014/01/autostart-application-at-bootup-in-android/

Using a broadcast receiver for alarm manager and notification

I created an alarm clock app that plays music when the alarm goes off. I set up a notification as well. I am using a broadcast receiver but it is ended when I leave the activity that created it and plays the music. My first question is, is a broadcast receiver the best way to handle this and is implementing a receiver in the manifest the only way to eliminate my problem of losing my receiver. When I leave the activity that created the app, I receive an error because I haven't unregistered the receiver. My first app and needing some advice. I can provide the code if needed.
You could use the inbuilt alarm to trigger using a pendingintent. That way you don't need a broadcast receiver.

turn on the switched off phone programatically and send a message at a particular time.Is it possible?

I am doing a project in which i have to send a message at a particular time.that function is already in many mobile apps.But what i want is even if the mobile is switch off,i want to turn it on programatically at a particular time and send the message .
Use AlarmManager with an RTC_WAKEUP alarm to get control at your specified time. Have the AlarmManager trigger a BroadcastReceiver via the PendingIntent you use when setting up the alarm in AlarmManager. Have the BroadcastReceiver send the message.

Categories

Resources