简体   繁体   中英

Notification is not opening Activity onClick

I have AlarmNotification in my app and everytime it appeared I want to click on it and open parent Activity/wakeup app from background. Why it doesnt work? It will show notification in certain time so alarm part is working as intended, but It will not do any action on click.

First I have to create notification:

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)
    }

Then I need to register Alarm to show notification in certain time:

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
    }

Then It will trigger BroadcastReceiver to open 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)
        }
    }
}

First you need to declare an Intent like:

Intent notificationClickIntent = new Intent("intent_id")

Then you need to declare a Pending Intent like this:

PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, notificationClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

In your notification builder add set 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)

In your manifest declare an intent filter on the activity you want to open (as I remember I could not make it work for the launcher activity, maybe it can work, I just gave up).

<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>

And last but not least, if activity hasn't been opened, notification onClick will trigger the onCreate method, if it has been opened, it will trigger the onNewIntent

hope this helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM