Локальное уведомление не отображается в Android 10 ⇐ Android
-
Гость
Локальное уведомление не отображается в Android 10
I was just creating a simple notification using chatgpt. but code not works in android 11. but it works in Android 22.
AndroidManifest.xml
MainActivity
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //set up alarm val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val intent = Intent(this, AlarmReceiver::class.java) val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) //Set the alarm to start at 8:00 AM val calendar = Calendar.getInstance() calendar.timeInMillis = System.currentTimeMillis() calendar.set(Calendar.HOUR_OF_DAY, 13) calendar.set(Calendar.MINUTE, 58) Log.e("Mahi", "1") //schedule the alarm alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) } } BroadcastReceiver
package com.mahi.a6amalarmex import android.Manifest import android.app.NotificationChannel import android.app.NotificationManager import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.os.Build import android.util.Log import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat class AlarmReceiver : BroadcastReceiver() { companion object { private const val CHANNEL_ID = "NotificationChannel" private const val NOTIFICATION_ID = 1 private const val REPEAT_INTERVAL = 10 * 60 * 1000L // 10 minutes } override fun onReceive(context: Context?, p1: Intent?) { Log.e("Mahi", "2") //code to show notification showNotification(context, "Law of detachment", "The law" ) } private fun showNotification(context: Context?, title: String, message: String){ val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager Log.e("Mahi", "3") // Create notification channel (for Android 8.0 and above) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( CHANNEL_ID, "Channel Name", NotificationManager.IMPORTANCE_DEFAULT ) // val notificationManager = // getSystemService(NotificationManager::class.java) notificationManager.createNotificationChannel(channel) Log.e("Mahi", "4") } //create notification val builder = NotificationCompat.Builder(context, "default") .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_DEFAULT) Log.e("Mahi", "5") if (ActivityCompat.checkSelfPermission( context, Manifest.permission.POST_NOTIFICATIONS ) != PackageManager.PERMISSION_GRANTED ) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return } Log.e("Mahi", "6") //Show notification notificationManager.notify(NOTIFICATION_ID, builder.build()) } }
Источник: https://stackoverflow.com/questions/780 ... android-10
I was just creating a simple notification using chatgpt. but code not works in android 11. but it works in Android 22.
AndroidManifest.xml
MainActivity
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //set up alarm val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val intent = Intent(this, AlarmReceiver::class.java) val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) //Set the alarm to start at 8:00 AM val calendar = Calendar.getInstance() calendar.timeInMillis = System.currentTimeMillis() calendar.set(Calendar.HOUR_OF_DAY, 13) calendar.set(Calendar.MINUTE, 58) Log.e("Mahi", "1") //schedule the alarm alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) } } BroadcastReceiver
package com.mahi.a6amalarmex import android.Manifest import android.app.NotificationChannel import android.app.NotificationManager import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.os.Build import android.util.Log import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat class AlarmReceiver : BroadcastReceiver() { companion object { private const val CHANNEL_ID = "NotificationChannel" private const val NOTIFICATION_ID = 1 private const val REPEAT_INTERVAL = 10 * 60 * 1000L // 10 minutes } override fun onReceive(context: Context?, p1: Intent?) { Log.e("Mahi", "2") //code to show notification showNotification(context, "Law of detachment", "The law" ) } private fun showNotification(context: Context?, title: String, message: String){ val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager Log.e("Mahi", "3") // Create notification channel (for Android 8.0 and above) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( CHANNEL_ID, "Channel Name", NotificationManager.IMPORTANCE_DEFAULT ) // val notificationManager = // getSystemService(NotificationManager::class.java) notificationManager.createNotificationChannel(channel) Log.e("Mahi", "4") } //create notification val builder = NotificationCompat.Builder(context, "default") .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_DEFAULT) Log.e("Mahi", "5") if (ActivityCompat.checkSelfPermission( context, Manifest.permission.POST_NOTIFICATIONS ) != PackageManager.PERMISSION_GRANTED ) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return } Log.e("Mahi", "6") //Show notification notificationManager.notify(NOTIFICATION_ID, builder.build()) } }
Источник: https://stackoverflow.com/questions/780 ... android-10
Мобильная версия