Notifications

Understanding Android Notifications and their implementation

Introduction to Notifications

Android में notifications user को important information और updates provide करते हैं। ये app के outside भी display हो सकते हैं और user engagement को increase करते हैं।

Notifications in Android provide users with important information and updates. They can be displayed outside the app and help increase user engagement.

Key Features (मुख्य विशेषताएं):

  • Notifications user को important updates provide करते हैं
  • Notifications provide important updates to users
  • Notifications को customize किया जा सकता है
  • Notifications can be customized
  • Notifications को different channels में organize किया जा सकता है
  • Notifications can be organized in different channels
  • Notifications को schedule किया जा सकता है
  • Notifications can be scheduled

Types of Notifications (नोटिफिकेशन्स के प्रकार)

Android में notifications के main types:

Main types of notifications in Android:

Type (प्रकार) Description (विवरण)
Basic Notifications Simple text और icon वाले notifications
Basic Notifications Simple notifications with text and icon
Expanded Notifications More content और actions वाले notifications
Expanded Notifications Notifications with more content and actions
Media Notifications Media playback controls वाले notifications
Media Notifications Notifications with media playback controls

Implementation Example (इम्प्लीमेंटेशन उदाहरण)

Notifications को implement करने का example:

Example of implementing notifications:

// Create notification channel (required for Android 8.0 and above)
private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = "My Channel"
        val descriptionText = "Channel Description"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) 
            as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

// Create and show notification
private fun showNotification() {
    val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle("Notification Title")
        .setContentText("Notification Content")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setAutoCancel(true)
        .addAction(R.drawable.ic_action, "Action", pendingIntent)

    val notificationManager = NotificationManagerCompat.from(this)
    notificationManager.notify(NOTIFICATION_ID, builder.build())
}

// Handle notification click
private val pendingIntent: PendingIntent
    get() {
        val intent = Intent(this, MainActivity::class.java)
        return PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_IMMUTABLE
        )
    }

Best Practices (सर्वोत्तम प्रथाएं)

Notifications के साथ काम करने के best practices:

Best practices for working with notifications:

  • Relevance: Only important notifications भेजें
  • Relevance: Send only important notifications
  • Timing: Appropriate time पर notifications भेजें
  • Timing: Send notifications at appropriate times
  • Customization: User preferences को respect करें
  • Customization: Respect user preferences
  • Testing: Different devices पर notifications को test करें
  • Testing: Test notifications on different devices