android - 通知未打开 Activity onClick

标签 android broadcastreceiver android-notifications android-pendingintent

我的应用程序中有 AlarmNotification,每次它出现时我都想点击它并从后台打开父 Activity/唤醒应用程序。为什么它不起作用?它会在特定时间显示通知,因此警报部分按预期工作,但它不会在点击时执行任何操作。

首先我必须创建通知:

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开启Notification:

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)

在你的 list 中声明一个你想要打开的 Activity 的 Intent 过滤器(我记得我不能让它适用于启动器 Activity ,也许它可以工作,我只是放弃了)。

<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

希望这有帮助:)

关于android - 通知未打开 Activity onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58535296/

相关文章:

c# - Xamarin Forms 图像未显示在 ListView 中

android - 如何从 BroadCastReceiver 更新 Activity 的 UI

Android 通知 setSound 不工作

android - 查找 AlertDialog 的 View

android - 将 Map<String, String> 保存到房间数据库的方法

android - 声明为不可为 null 的 Kotlin 属性即使已初始化值也可以为 null

Android连接更改广播顺序

android - 在android中更改位置时如何使用广播接收器跟踪触发服务?

java - 如何在 Android 中安排通知

android - Notification 操作启动的 IntentService 在哪个线程中运行?