Tuesday, January 10, 2017

Set timer with interval at starting on boot, Show notification on proper event/condition

First of all, we create Class that starts at Boot

public class OnBootReceiver extends BroadcastReceiver {

 @Override public void onReceive(Context context, Intent intent) {

  AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, OnBootCalling.class);
  PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.HOUR_OF_DAY, Constant.alarmHour);
  calendar.set(Calendar.MINUTE, Constant.alarmMinute);

  alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
   Constant.alarmInterval, alarmIntent);

 }

and add this to your androidManifest.xml

<receiver    android:name=".OnBootReceiver"    android:enabled="true">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />

        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <action android:name="android.intent.action.REBOOT" />
    </intent-filter>
</receiver>

So, everytime, On specific time, as set in Alarm, will call OnBootCalling.class

public class OnBootCalling extends WakefulBroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {

  if (conditionIsSatisfied)
   ComponentName comp = new ComponentName(context.getPackageName(),
    AlarmService.class.getName());
  startWakefulService(context, (intent.setComponent(comp)));
  setResultCode(Activity.RESULT_OK);
  break;
 }
}
}
}
}

which will call AlarmService.class, which will call notification

public class AlarmService extends IntentService {

 public AlarmService() {
  super("AlarmService");
 }

 @Override
 public void onHandleIntent(Intent intent) {

  //If you want to play default sound with notification 
  Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  Ringtone ringtone = RingtoneManager.getRingtone(PlayerApp.getContext(), alarmUri);
  ringtone.play();

  Intent intent = new Intent(PlayerApp.getContext(), MainActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);

  Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(PlayerApp.getContext())
   .setSmallIcon(R.drawable.ic_music)
   .setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_music))
   .setContentTitle("Alert")
   .setTicker("Ticking Alert")
   .setStyle(new NotificationCompat.BigTextStyle().bigText("For BigView, Your Message comes here"))
   .setContentText("Your Message comes here")
   .setAutoCancel(true)
   .setSound(defaultSoundUri)
   .setContentIntent(pendingIntent);
  //                .addAction (R.drawable.ic_menu_manage,
  //                        getString(R.string.anyaction), pendingIntent);

  NotificationManager notificationManager =
   (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

  notificationManager.notify(401, notificationBuilder.build());
 }
}

Note: Make sure you get proper context

Do not forget to add these lines to your Manifest

<receiver android:name=".OnBootCalling" />

<service android:name=".AlarmService" android:enabled="true" />

Also add these permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Hope, this helps you somewhere.

No comments:

Post a Comment