I'd like to show my users a notification at specific times every day. I've looked through tons of examples and questions, but I still have a problem - the notification just won't show up.
This is the BroadcastReceiver class -
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
private Context con;
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
con = context;
showNotification();
}
public void SetAlarm(Context context)
{
Calendar now = Calendar.getInstance();
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 30 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 1000 * 5 , pi);
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
/**
* Creates a notification and shows it in the OS drag-down status bar
*/
private void showNotification() {
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(con)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Hello")
.setContentText("Hello")
.setDefaults(Notification.DEFAULT_ALL)
.setTicker("HELLO!")
.setAutoCancel(true);
Intent resultIntent=new Intent(con, ViewMenu.class);
PendingIntent pIntent=PendingIntent.getActivity(con,0,resultIntent,0);
mBuilder.setContentIntent(pIntent);
NotificationManager mNotificationManager =
(NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(90, mBuilder.build());
Toast.makeText(con, "TEST4", Toast.LENGTH_LONG).show();
}
}
And this this how I set it up on the main Activity -
alarm = new testy.app.Notifications.AlarmManagerBroadcastReceiver();
alarm.SetAlarm(this);
I've also registered the BroadcastReceiver in the Manifest -
<!-- Broadcast receiver -->
<receiver android:name="testy$app$Notifications$AlarmManagerBroadcastReceiver</receiver>
Will appreciate any suggestion, thanks