I want to fire notification for my adhan, So I used Alarmmanager and JobIntentService but I am not getting notification.
Here is receiver.
<receiver android:name="receiver.BootReceiver"
android:enabled="true"
android:exported="true">
</receiver>
<service
android:name=".services.JobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />
and following is service class extending JobIntentService.
public class JobService extends JobIntentService {
public static final int JOB_ID = 100;
public static final int NOTIF_ID = 56;
long timestamp;
private NotificationManager mNotificationManager;
// private final IBinder mBinder = new NotificationService.LocalBinder();
// MediaPlayer mMediaPlayer = null;
String MpStop;
SharedPreferences mPreferences;
float selectedLat, selectedLng;
String name, userSect;
boolean mAzanNotification;
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, JobService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
mPreferences = Application.getAppContext().getSharedPreferences(Utils.PREF_NAME, Context.MODE_PRIVATE);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notifyTasker.run();
}
In notifyTasker isTimerTask(), where I check if currentTime equals my prayer Time. If it is true then I fire Notification.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
.setSmallIcon(R.mipmap.icon_beta)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.icon_beta))
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setContentTitle(waktu)
.setAutoCancel(true) // .setSound(uri)
.setContentIntent(contentIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
/* AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();*/
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel("CH_ID", "Testing_Audio", NotificationManager.IMPORTANCE_HIGH); // notificationChannel.setSound(uri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
Notification notification = notificationBuilder.build();
notification.flags |=
Notification.FLAG_ONGOING_EVENT |
Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 300;
notification.ledOffMS = 3000;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
And following is my alarmManager which I activate from activity.
Intent intent = new Intent(getMain(), BootReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
alarmPendingIntent = PendingIntent.getBroadcast(getMain(), BootReceiver.REQUEST_CODE,
intent, 0);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int intervalMillis = 60000; // as of API 19, alarm manager will be forced up to 60000 to save battery
AlarmManager am = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
// See https://developer.android.com/training/scheduling/alarms.html
int minutes = 1;
// by my own convention, minutes <= 0 means notifications are disabled
// if (minutes > 0) {
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() + minutes * 60 * 1000, minutes * 60 * 1000, alarmPendingIntent);
I tried to fire notification with above code for android oreo but it is not working. Is there any possibility to fire notification at specific time using job schedular. As notification will be fired at specific azan time.