android - 通知未打开 Activity onClick
问题描述
我的应用程序中有 AlarmNotification,每次出现时我都想单击它并从后台打开父 Activity/wakeup 应用程序。为什么它不起作用?它会在特定时间显示通知,因此警报部分按预期工作,但它不会在点击时执行任何操作。
首先我必须创建通知:
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)
}
然后我需要注册警报以在特定时间显示通知:
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
打开通知:
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,例如:
Intent notificationClickIntent = new Intent("intent_id")
然后你需要像这样声明一个 Pending Intent:
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)
在您的清单中声明您要打开的活动的意图过滤器(因为我记得我无法使其适用于启动器活动,也许它可以工作,我只是放弃了)。
<activity
android:name="Activity_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar.LightStatusBar">
<intent-filter>
<action android:name="intent_id" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
最后但同样重要的是,如果activity没有打开,通知onClick会触发onCreate方法,如果它已经打开,会触发onNewIntent
希望这可以帮助 :)
推荐阅读
- python - 从输出中查找哈希加密输入
- java - 来自现有文件的 NetBeans GUI Builder
- visual-studio-code - 无法在 VS Code 中启动客户端 Azure 资源管理器模板
- excel - 邮件合并文件..!根据员工姓名(如 SMS 线程)分组并保存为一种 PDF 格式
- matlab - 使用 fprintf 将字符串和表格合并到一个文本文件 - Matlab
- python - 在python中遇到问题:显示错误只能将str(不是“bool”)连接到str
- node.js - 在使用 node-ftech 时,我如何处理可能阻止 Jest 退出的 JEST 句柄
- android - WebView 不显示 KOLIBRI 登录页面
- javascript - 在 ReactJS 中单击按钮时通过 id 动态加载数据
- javascript - looping through object in the DOM