Android 警报管理器未触发广播接收器
我正在设置一个项目,我想使用广播接收器和报警管理器推送通知。由于某种原因,广播接收器没有被触发,即使我把日志检查,我确认它不工作Android 警报管理器未触发广播接收器,android,kotlin,notifications,Android,Kotlin,Notifications,我正在设置一个项目,我想使用广播接收器和报警管理器推送通知。由于某种原因,广播接收器没有被触发,即使我把日志检查,我确认它不工作 这是我的听筒课 这是我的applicat类,我为oreo版本的android添加了频道 这是我的清单文件 任何帮助都将不胜感激 class MyReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent?) {
- 这是我的听筒课
- 这是我的applicat类,我为oreo版本的android添加了频道
- 这是我的清单文件
任何帮助都将不胜感激
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
createNotificatio(context.applicationContext)
}
private fun createNotificatio(context: Context?) {
val intent = Intent(context,MyReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context,0,intent,0)
val alarmManager = context?.getSystemService(Context.ALARM_SERVICE) as AlarmManager
when {
Build.VERSION.SDK_INT >= 23 -> {
//Alarm Manager to get broadcast receiver
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),pendingIntent)
}
Build.VERSION.SDK_INT in 21..22 -> {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),pendingIntent)
}
}
//Notification code
val notification = NotificationCompat.Builder(context!!,"id")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText("this is a simple text")
.setContentTitle("title")
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setPriority(NotificationManagerCompat.IMPORTANCE_HIGH)
.build()
NotificationManagerCompat.from(context).notify(1,notification)
}
}
class NotificationApplication : Application() {
// i have the application class into the manifest file
override fun onCreate() {
super.onCreate()
notification()
}
private fun notification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
val channel = NotificationChannel(
"string","id", NotificationManager.IMPORTANCE_HIGH
)
var notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kiz.notes.projecttodelete">
<application
android:name=".NotificationApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver"/>
</application>
</manifest>