I read several questions here, and have a similar one as well. Non of the answers on another questions manage to solve the issue unfortunately. I know how to create a notification on Button click, yet I failed to create a notification for a specific time. My goal in the following code snippet was to create a notification at 19:00 every day. Here is the code snippet:
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
setAlarm(calendar);
}
public void setAlarm(Calendar calendar) {
Intent alertIntent = new Intent(this, Receiver.class);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, alertIntent, 0);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 00);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*60*60*1000, pendingIntent);
}
Receiver:
public class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "Blabla", "Blablablabla", "Alert");
}
public void createNotification(Context context, String msg,
String msgText, String msgAlert) {
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.clear_dayicon)
.setContentTitle(msg)
.setContentText("msgText")
.setTicker(msgAlert);
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
Manifest:
// only ones I called after started working with notifications and stuff
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".Receiver2" android:enabled="true">
<intent-filter> <action android:name="NOTIFICATION_SERVICE" /></intent-filter>
</service>
<receiver android:name=".Receiver"/>
Some help would be super appreciated. Since I'm a beginner, I need someone to show me my mistakes. Otherwise, I can't spot them no matter how many docs I read. This code looks fine to me :P
try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 00);
setAlarm(calendar);
}
public void setAlarm(Calendar calendar) {
boolean alarmNoActive = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);
if(alarmNoActive){
Intent itAlarm = new Intent("ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,itAlarm,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*60*60*1000, pendingIntent);
}
}
In the manifest put this permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"
In application:
<receiver
android:name="path of the BroadcastReceiver.Receiver"
android:label="BroadcastReceiverAux">
<intent-filter>
<action android:name="ALARM" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>