Reputation: 11
I am developing an app that fires notifications in the background. Even when the device is on sleep. I have used the alarm manager and service class. Here is the code- These are in the activity class.
public void start(){
scheduleNotification(getNotification(w.getMEAN()),time);
}
private void scheduleNotification(Notification notification, int delay) {
AlarmManager alarmManager = (AlarmManager)getSystemService(getBaseContext().ALARM_SERVICE);
int d=delay*1000;
Intent notificationIntent = new Intent(getApplicationContext(), NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + d;
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
private Notification getNotification(String content) {
Intent intent=new Intent(getApplicationContext(),MeaningActivity.class);
id++;}
PendingIntent pi=PendingIntent.getActivity(this, 10, intent,PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
if(id<15){
builder.setContentIntent(pi);
builder.setAutoCancel(true);
notiId++;
}
return builder.build();
}
The service is called by intent on button click
case R.id.button1:
{
String t=ed.getText().toString();
int time = Integer.parseInt(t);
savePreferences("switch", s.isChecked());
if (s.isChecked())
{
savePreferences("time", ed.getText().toString());
Intent intent = new Intent(this,NotifyService.class);
//intent.putExtra("time",time);
startService(intent);
}
else
{
stopService(new Intent(this,NotifyService.class));
}
This is the notification publisher.java-
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
List<Word> quesList;
int id=0,count =0;
Word w;
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
The Service Class is-
@Override
public IBinder onBind(Intent arg0) {
//time = arg0.getIntExtra("time",0);
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//start();
// Restart the service if got killed
sa.start();
Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"Service destroyed", Toast.LENGTH_LONG).show();
}
The above code shows an error stating the service cannot be called by intent!! Please help me as to how can I fire the notifications even in the background at specified time.. Thanks in advance..
Upvotes: 0
Views: 146
Reputation: 351
We have similar cases but mine works. I'll post here my code and you adapt it to yours. What you are doing wrong is doing the service "job" in broadcastreceiver and the broadreceiver "job" inside service
MainActivity Inside onCreate
Intent myIntent = new Intent(MainActivity.this, BroadReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
/*alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);*/
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
BroadcastReceiver
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotifyService.class);
context.startService(service1);
Service
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationManager notificationManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new NotificationCompat.Builder(this)
.setTicker(getText(R.string.notification_ticker))
.setSmallIcon(R.drawable.green)
.setContentTitle(frase())
.setContentText(getText(R.string.notification_text))
.setAutoCancel(true)
.build();
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationManager.notify(0, notification);
return START_STICKY;
}
Upvotes: 0