Android 通知未在单击时打开活动
我的应用程序中有AlarmNotification,每次它出现时,我都想单击它并从后台打开父活动/唤醒应用程序。为什么它不起作用?它将在特定时间显示通知,以便报警部件按预期工作,但不会在单击时执行任何操作 首先,我必须创建通知:Android 通知未在单击时打开活动,android,broadcastreceiver,android-notifications,android-pendingintent,Android,Broadcastreceiver,Android Notifications,Android Pendingintent,我的应用程序中有AlarmNotification,每次它出现时,我都想单击它并从后台打开父活动/唤醒应用程序。为什么它不起作用?它将在特定时间显示通知,以便报警部件按预期工作,但不会在单击时执行任何操作 首先,我必须创建通知: private fun createNotification(){ val vibrateFreq = longArrayOf(1000, 1000, 1000, 1000, 1000) nb = NotificationCompat.B
private fun createNotification(){
val vibrateFreq = longArrayOf(1000, 1000, 1000, 1000, 1000)
nb = NotificationCompat.Builder(a, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(text)
.setVibrate(vibrateFreq)
.setStyle(NotificationCompat.BigTextStyle().bigText(textLong))
.setAutoCancel(true)
}
class AlarmReceiver: BroadcastReceiver(){
companion object{
const val NOTIFICATION_ID = "20288855447-99899"
const val NOTIFICATION = "alarmNotify"
}
override fun onReceive(context: Context?, intent: Intent?) {
val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification: Notification? = intent?.getParcelableExtra(NOTIFICATION)
val id = intent?.getIntExtra(NOTIFICATION_ID, 0)
id?.let {
notificationManager.notify(id, notification)
}
}
}
然后我需要注册报警以在特定时间显示通知:
private fun createAlarm(){
val pInt = app.createAlarmPendingIntent(a, nb)
val cal = Calendar.getInstance()
cal.add(Calendar.MINUTE, delta)
app.aManager.set(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pInt)
}
fun createAlarmPendingIntent(a: Activity, nb: NotificationCompat.Builder?): PendingIntent{
val nInt = Intent(a, AlarmReceiver::class.java)
nInt.putExtra(
AlarmReceiver.NOTIFICATION_ID,
AlarmNotification.NOTIFICATION_NAME
)
nInt.putExtra(AlarmReceiver.NOTIFICATION, nb?.build())
val pInt = PendingIntent.getBroadcast(
a,
0,
nInt,
PendingIntent.FLAG_UPDATE_CURRENT
)
return pInt
}
然后它将触发BroadcastReceiver
打开通知:
private fun createNotification(){
val vibrateFreq = longArrayOf(1000, 1000, 1000, 1000, 1000)
nb = NotificationCompat.Builder(a, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(text)
.setVibrate(vibrateFreq)
.setStyle(NotificationCompat.BigTextStyle().bigText(textLong))
.setAutoCancel(true)
}
class AlarmReceiver: BroadcastReceiver(){
companion object{
const val NOTIFICATION_ID = "20288855447-99899"
const val NOTIFICATION = "alarmNotify"
}
override fun onReceive(context: Context?, intent: Intent?) {
val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification: Notification? = intent?.getParcelableExtra(NOTIFICATION)
val id = intent?.getIntExtra(NOTIFICATION_ID, 0)
id?.let {
notificationManager.notify(id, notification)
}
}
}
首先,您需要声明如下意图:
Intent notificationClickIntent = new Intent("intent_id")
然后您需要声明一个挂起的意图,如下所示:
PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, notificationClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
在通知生成器中,添加集合contentIntent:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(notificationData.getTitle())
.setContentText(notificationData.getBody())
.setPriority(NotificationCompat.PRIORITY_HIGH)
**.setContentIntent(notificationClickPendingIntent)**
.addAction(R.drawable.ic_notification, context.getString(R.string.turn_on_alarms), notificationButtonClickPendingIntent)
.setAutoCancel(true)
在您的清单中,对要打开的活动声明一个意图过滤器(因为我记得我无法使它对启动程序活动起作用,也许它可以起作用,我只是放弃了)
最后但并非最不重要的一点是,如果活动尚未打开,notification onClick将触发onCreate方法,如果它已打开,则将触发onNewIntent
希望这有帮助:)从哪里调用/传递家长活动/唤醒应用程序?默认情况下,将触发主页/启动器屏幕。您需要根据参数打开所需的屏幕。每次启动程序活动中都会创建此通知。它假设每次都会打开启动器活动。我必须把这个
pendingent pendingent=pendingent.getActivity(上下文、请求代码、通知ClickIntent、pendingent.FLAG_UPDATE_CURRENT)放在当前位置
进入到onReceive
方法中的意图中,它就起作用了。因为第一个PendingEvent用于触发BroadcastReceiver的通知,而第二个PendingEvent必须是活动意图。