So i have a problem with my notification. it doesn't tiger. So if somebody could help me with this.
here is the MainActivity:
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24 , pendingIntent);
The Service class
public class NotifyService extends Service {
#Override
public void onCreate() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, builder.build());
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
my Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.berilo.dayabs" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ChalangesMenu"
android:label="#string/title_activity_chalanges_menu" >
</activity>
<activity
android:name=".Workout"
android:label="#string/title_activity_workout" >
</activity>
</application>
the min Sdk Version is 14
So i set the AlarmManager to trigger at midnight. i set the time on my phone but nothing happens.
Your NotifyService is not declared in manifest file.
declare it like
<service android:name=".NotifyService">
</service>
Related
I'm trying to make my application use AlarmManager to send a notification after 5 seconds, but the pendingIntent doesn't seem to get called.
Heres my code
In onCreate in MainActivity:
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
Intent alertIntent = new Intent(this, Notification.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime , PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Notification.class:
public class Notification extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setTicker("Ticker")
.setContentText("Text");
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
Android Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wouter.lifetracker"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Notification"
>
</receiver>
</application>
i have searched for hours but i can't find out why nothing is happening
See: https://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent)
Note: Beginning in API 19, the trigger time passed to this method is
treated as inexact: the alarm will not be delivered before this time,
but may be deferred and delivered some time later. The OS will use
this policy in order to "batch" alarms together across the entire
system, minimizing the number of times the device needs to "wake up"
and minimizing battery use. In general, alarms scheduled in the near
future will not be deferred as long as alarms scheduled far in the
future.
only 5 seconds?
try this:
new Handler().postDelayed(new Runnable() {
public void run() {
showNotification(); //your notification code called after 5 seconds
}
}, 5 * 1000); // 5 seconds...
OR TRY:
long alertTime = System.currentTimeMillis() + (5 * 1000); // <-- try this
Intent alertIntent = new Intent(this, Notification.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime , PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
I am almost done, but when clicking the set Alarm button to fire the notification after 5 seconds for example, nothing happens.
this is the OnClick method for SetAlarm
public void setAlarm(View view) {
long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
Intent alertIntent = new Intent(this, AlertReceiver.class);
//schedule to happen at later date
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
and this is my receiver:
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "Times up", "5 seconds has passed", "Alert");
}
public void createNotification(Context context, String msg, String msgText, String msgAlert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
//intent to fire when notification clicked on
mBuilder.setContentIntent(notificIntent);
//how the person will be notified
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//cancel notification when clicked in the taskbar
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1,mBuilder.build());
}
and this is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yahyazeid.testnotification" >
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".AlertReceiver">
<intent-filter> <action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MoreInfoNotification"
android:label="More on notification"
android:parentActivityName=".MainActivity">
</activity>
</application>
</manifest>
Any suggestions?
The corrected manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yahyazeid.testnotification" >
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MoreInfoNotification"
android:label="More on notification"
android:parentActivityName=".MainActivity">
</activity>
<service android:name=".AlertReceiver">
</service>
</application>
</manifest>
Try this code. It looks like you did not set the notification in your receiver.
public void scheduleAlarm(View V)
{
// time at which alarm will be scheduled here alarm is scheduled at 1 day from current time,
// we fetch the current time in milliseconds and added 1 day time
// i.e. 24*60*60*1000= 86,400,000 milliseconds in a day
Long time = new GregorianCalendar().getTimeInMillis()+5*1000;
// create an Intent and set the class which will execute when Alarm triggers, here we have
// given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
Intent intentAlarm = new Intent(this, AlarmReciever.class);
//Get the Alarm Service
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Set for 5 seconds", Toast.LENGTH_LONG).show();
Then for the Receiver
public class Receiver extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.depressiontest)
.setContentTitle("Time has Expired")
.setContentText("5 Seconds has passed.")
.setVibrate(pattern)
.setAutoCancel(true);
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
And then in your manifest
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".Receiver">
<intent-filter> <action android:name="NOTIFICATION_SERVICE" />
</intent- filter>
</service>
For setting a specific date using the get Calendar instance
Calendar cal = Calendar.getInstance();
//Setting date for 11/29/2016
cal.set(Calendar.DATE,29); //1-31 days of week
cal.set(Calendar.MONTH,10); //January starts at 0 (zero), December = 11
cal.set(Calendar.YEAR,2016);//year...
//Setting the specific hour/min/sec
cal.set(Calendar.HOUR_OF_DAY, 16); //HOUR
cal.set(Calendar.MINUTE, 39); //MIN
cal.set(Calendar.SECOND, 10); //SEC
// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);
//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
I am building an android app to display recurring scheduled notification at a specified time of the day.
For this I have created broadcastReceiver using following code:
public class ScheduleNotification extends BroadcastReceiver {
public static final int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) {
long when = System.currentTimeMillis();
MainActivity mainActivity = new MainActivity();
String _pasuram_number = mainActivity.get_pasuram_number();
String[] _pasuram_str = mainActivity.get_dd_text(_pasuram_number).split(",");
Log.d("VC", "Notification intent paasuram " + _pasuram_number);
intent.putExtra("pasuramnumber", _pasuram_number);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent snoozeIntent = new Intent(context, MainActivity.class);
PendingIntent piSnooze = PendingIntent.getService(context, 0, snoozeIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_stat_name);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_name));
builder.setContentTitle("Title of the notification");
builder.setContentText(_pasuram_str[9]+"-"+_pasuram_str[11]);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(_pasuram_str[0] + "-" + _pasuram_str[8]));
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
In the MainActivity added code to create alarm
private void createScheduledNotification(int days)
{
// 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.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
//calendar.add(Calendar.HOUR_OF_DAY, days * 24);
// 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 = (int) System.currentTimeMillis();
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, ScheduleNotification.class);
// Prepare the pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 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.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
//alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
The code compiles fine and application runs, but scheduled notification does not appear as expected.
In the manifest file added receiver as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vaishnavism.eclass.dinamorudivyaprabandam" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- permission required to use Alarm Manager -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<!-- Register the Alarm Receiver -->
<receiver android:name="com.vaishnavism.eclass.dinamorudivyaprabandam.ScheduleNotification"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.vaishnavism.eclass.dinamorudivyaprabandam.MainActivity" />
</activity>
</application>
</manifest>
Any help to resolve the issue is greatly appreciated.
Thanks
Try to use set your alarm like below :
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
int interval = 1000 * 60 * 60 * 24;
Intent myIntent = new Intent(yourActivity.this, ScheduleNotification .class);
pendingIntent = PendingIntent.getBroadcast(UserDashBoardActivity.this, 0, myIntent,0);
/* Repeating on every 24 hours interval */
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
And you need to start an Alarm When the Device Boots.
For more info refere here.
Hope it will help you.
Here is my code below, what is the problem? If i look at log, there is no start of AlarmReceiver class. So no notification is sent to user. I tried every minute in loop and still nothing happened. What am i doing wrong?
Thanks in Advance!!
public void setRepeatingAlarm(int hour, int min)
{
Intent myIntent = new Intent(this , AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}
//Receiver Class AlarmReceiver
public void onReceive(Context context, Intent intent)
{
taskDb = new TaskDb(context);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
String temp ="";
for(String s: taskDb.selectTodayTasks())
{
temp +="-" + s + "\n";
}
if(!temp.equals("")) {
Notification n = new Notification.Builder(context)
.setContentTitle(context.getString(R.string.today_tasks))
.setContentText(temp)
.setLights(Color.CYAN,500,500)
.setContentIntent(pIntent)
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(MainActivity.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
}
//android manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".main.MainActivity"
android:windowSoftInputMode="stateHidden"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".background.AlarmReceiver" android:enabled="true">
</receiver>
</application>
Change PendingIntent.getService to PendingIntent.getBroadcast:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
Because PendingIntent.getService is used to Retrieve a PendingIntent that will start a service,... but PendingIntent.getBroadcast for Retrieve a PendingIntent that will perform a broadcast,...
I have a simple application, when user toggle the alarm settings on,
my application will schedule a task to show an activity (PopupActivity) at midnight.
The PopupActivity is not the main activity of my application. I have tested on some devices and found that there is no problems.
However, some of my users reported that when the alarm trigger, my application will launch both the PopupActivity and the MainActivity.
How can I avoid the MainActivity launched when the alarm is trigger?
The following is my code snap:
public void scheduleAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
AlarmReceiver.java:
#Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent();
newIntent.setClass(context, PopupActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.putExtra(PopupActivity.KEY_MESSAGE, "Popup");
context.startActivity(newIntent);
}
AndroidManifest.xml:
...
<activity
android:name=".MainActivity"
android:label="#string/APP_NAME"
android:configChanges="orientation|locale"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AlarmReceiver" />
<activity
android:name=".PopupActivity"
android:label="#string/APP_NAME"
android:configChanges="orientation|locale"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
...