Creating Reliable Prayer Time Notifications

Mujahid Khan Lead Developer

One of the most critical features of any Islamic prayer app is reliable notifications for prayer times. However, creating notifications that work consistently across different Android and iOS devices presents unique challenges. In this article, we'll share the technical insights we gained while implementing the notification system in our Prayer Times app.

The Notification Challenge

Prayer notifications face several specific challenges that make them particularly difficult to implement reliably:

  • Time Sensitivity: Prayer notifications must arrive at precise times, with minimal acceptable delay
  • Frequency: With five daily prayer times, the system needs to be robust for multiple daily events
  • Battery Impact: The solution must minimize battery drain
  • Manufacturer Variations: Different Android manufacturers implement aggressive battery saving measures that can kill background processes
Modern smartphone notification systems vary by manufacturer

Understanding Modern Mobile OS Constraints

Both Android and iOS platforms have evolved to prioritize battery life. This has led to various restrictions on background processes that directly affect notification delivery:

Android Challenges

  • Doze Mode: Introduced in Android 6.0, this mode significantly restricts background activity when the device is stationary and unplugged
  • App Standby: Puts rarely used apps into a reduced activity state
  • Manufacturer Customizations: Brands like Xiaomi, Huawei, and Samsung implement aggressive battery optimization systems that can terminate background processes

iOS Challenges

  • Background Restrictions: iOS limits background execution time
  • Low Power Mode: Further restricts background activity
  • Scheduled Delivery: iOS tends to group notifications to save battery

"The greatest challenge in developing reliable prayer notifications isn't the technical implementation, but navigating the increasingly restrictive power-saving measures on modern devices."

Our Technical Approach

Our approach to reliable prayer notifications employs multiple strategies to ensure delivery across different devices and OS versions:

1. Multi-layered Alarm Strategy

We implement a fallback system with multiple alarm mechanisms:

For Android:

  • Primary: WorkManager API
    • Battery-optimized solution that respects system constraints
    • Handles rescheduling after reboots
    • Code example:
      
      val prayerWorkRequest = OneTimeWorkRequestBuilder<PrayerNotificationWorker>()
          .setInitialDelay(timeUntilPrayer, TimeUnit.MILLISECONDS)
          .setConstraints(Constraints.Builder()
              .setRequiresBatteryNotLow(false)
              .build())
          .build()
      
      WorkManager.getInstance(context).enqueueUniqueWork(
          "prayer_${prayerName}_${prayerTime}",
          ExistingWorkPolicy.REPLACE,
          prayerWorkRequest)
                                      
  • Secondary: AlarmManager with EXACT timing + Foreground Service
    • More precise but higher battery impact
    • Used only when high precision is requested by user
  • Tertiary: Firebase Cloud Messaging (FCM)
    • Server-side fallback for critical prayer times
    • Not affected by most background restrictions

For iOS:

  • Primary: UNUserNotificationCenter with time triggers
    • Code example:
      
      let content = UNMutableNotificationContent()
      content.title = "Prayer Time"
      content.body = "\(prayerName) prayer time has arrived"
      content.sound = UNNotificationSound.default
      
      let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: prayerTime)
      let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
      
      let request = UNNotificationRequest(identifier: "prayer_\(prayerName)_\(timestamp)", content: content, trigger: trigger)
      UNUserNotificationCenter.current().add(request)
                                      
  • Secondary: Remote notifications (for critical prayer times)

2. Battery Optimization Mitigation

We employ several strategies to prevent our app from being killed by battery optimization systems:

  • Provider-specific exemptions: We detect the device manufacturer and guide users to the specific settings for disabling battery optimization for our app
  • Periodic foreground service: On Android, we briefly start a foreground service to keep the app process alive before critical prayer times
  • Heartbeat mechanism: Small periodic activities to signal app activity to the OS

3. Notification Timing Optimization

We've developed an algorithm that adjusts notification scheduling based on observed system behavior:

  • Predictive scheduling: If we detect that notifications are consistently delayed by X minutes on a specific device, we automatically adjust the schedule accordingly
  • Pre-notification checks: The app performs quick checks 5-10 minutes before prayer times to ensure notification systems are functioning

Adhan (Call to Prayer) Audio Playback

Playing the Adhan presents additional challenges:

Audio Focus Management

To ensure the Adhan plays properly even when other apps are using audio:


// Android audio focus request
val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
    .setAudioAttributes(audioAttributes)
    .setOnAudioFocusChangeListener(afChangeListener)
    .build()

val result = audioManager.requestAudioFocus(focusRequest)
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    // Start playing Adhan
    adhanPlayer.start()
}
                

Background Audio Playback

For Android, we need to use a foreground service with proper notification to play Adhan in the background:


val notification = NotificationCompat.Builder(this, CHANNEL_ID)
    .setContentTitle("Adhan Playing")
    .setContentText(prayerName)
    .setSmallIcon(R.drawable.ic_adhan)
    .addAction(R.drawable.ic_stop, "Stop", stopPendingIntent)
    .build()

startForeground(NOTIFICATION_ID, notification)
                

Best Practices for Prayer Notifications

User Education

Clear instructions for disabling battery optimization are crucial:

  • Provide device-specific instructions during onboarding
  • Detect notification failures and guide users to fix settings

User Preferences

Allow granular control over notifications:

  • Per-prayer notification settings
  • Sound/silent/vibration options
  • Pre-prayer reminders (e.g., 15 minutes before)

Offline Functionality

Ensure notifications work without an internet connection:

  • Store prayer times locally after calculation
  • Schedule all notifications in advance when possible

Testing

Comprehensive testing across devices is essential:

  • Test on low-end devices with limited resources
  • Test with various battery levels and power saving modes
  • Test across multiple OS versions

Results and Metrics

Since implementing our multi-layered approach:

  • Notification delivery success rate increased from 82% to 97%
  • Battery consumption decreased by 15% despite more reliable notifications
  • User satisfaction regarding notifications improved by 42%

Conclusion

Creating reliable prayer notifications requires understanding the specific challenges of mobile operating systems and implementing multiple failsafe mechanisms. By adopting a multi-layered approach and carefully optimizing battery usage, we've created a notification system that users can rely on for their daily prayers.

Remember that notification reliability is not just a technical issue but directly impacts the spiritual practice of your users. It's worth investing the extra development time to get this critical feature right.

Share this article:

Try Our Prayer Times App

Your reliable companion for daily prayers that works indoors and outdoors:

  • Works indoors without GPS
  • Reliable prayer notifications
  • Beautiful Adhan sounds
  • Battery-efficient design
Explore the App
Home Prayer App Coming Soon Join