Step 1: Permission (Android 13 and Later)
xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
kotlin
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> /* ... */ }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Button(onClick = { launcher.launch(Manifest.permission.POST_NOTIFICATIONS) }) { Text("Enable reminders") }
}
Step 2: Create a Channel
Since Android 8 every notification belongs to a channel. Users can control each channel separately in Settings.
kotlin
const val REMINDER_CHANNEL = "reminders"
fun createNotificationChannel(context: Context) {
val channel = NotificationChannel(REMINDER_CHANNEL, "Reminders", NotificationManager.IMPORTANCE_DEFAULT).apply {
description = "Study reminders"
}
context.getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
}
// Call once, for example in Application.onCreate()
Step 3: Show a Notification
kotlin
@SuppressLint("MissingPermission")
fun showReminder(context: Context, title: String, text: String) {
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pending = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
val notification = NotificationCompat.Builder(context, REMINDER_CHANNEL)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(pending)
.setAutoCancel(true)
.addAction(R.drawable.ic_done, "Done", donePendingIntent)
.build()
if (NotificationManagerCompat.from(context).areNotificationsEnabled()) {
NotificationManagerCompat.from(context).notify(1001, notification)
}
}
PendingIntent flags: Always set FLAG_IMMUTABLE (or FLAG_MUTABLE only when you really need it). Missing flags cause a crash on recent Android versions.
Scheduling
For a reminder at a certain time use WorkManager for flexible timing, or AlarmManager with exact alarms only when a precise time is essential. Exact alarms need special permission handling on recent Android versions, so check the current documentation.
Push Notifications with FCM
- Add Firebase to your app and the Firebase Messaging library.
- Your app receives a registration token that identifies the device.
- Send the token to your backend.
- Your server or the Firebase console sends a message to FCM with that token (or to a topic).
- FCM delivers the message to the device.
kotlin
class MyMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
// send the new token to your server
}
override fun onMessageReceived(message: RemoteMessage) {
val title = message.notification?.title ?: message.data["title"] ?: "New message"
val body = message.notification?.body ?: message.data["body"] ?: ""
showReminder(this, title, body)
}
}
// Manifest
// <service android:name=".MyMessagingService" android:exported="false">
// <intent-filter><action android:name="com.google.firebase.MESSAGING_EVENT" /></intent-filter>
// </service>
kotlin
val token = Firebase.messaging.token.await()
Firebase.messaging.subscribeToTopic("news")
Good manners: Send useful notifications only. Let users choose categories with several channels and make them easy to turn off.
Conclusion
Notifications need a channel, a permission on new Android versions and a good PendingIntent. FCM delivers push messages with a device token.
Quick Summary: Create channels, ask POST_NOTIFICATIONS, build with NotificationCompat and use FirebaseMessagingService for push.