Search code examples
androidandroid-notificationsandroid-pendingintent

Passing Activity as Parameter in Notification


I have the notification defined as:

   private void sendNotification(String msg, Activity onClickActivity) {

        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

        // Send to the login activity, since if already a member and logged in, should be able to start that activity

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, onClickActivity), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.gcm_cloud)
                .setContentTitle("Hello")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    }

as you can see, I wish to be able to allow dynamic references to the PendingIntent, so that a user may click on notification and subsequently access different activities given Activity onClickActivity. At present the error notes it cannot resolve constructor.

How can I get this to work?


Solution

  • The Intent constructor expects a class, not an instance. Just change your method signature to

    private void sendNotification(String msg, Class<? extends Activity> onClickActivity) {