简体   繁体   中英

Start an Activity when clicking button in Notification

Currently I am building some notifications for my app. One use case is you get a friend request and I add two button to the notification to accept or reject the request from the notification if wanted. This is my code:

    Context ctx = TheGameApplication.getAppContext();

    Intent intent = new Intent(ctx, LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx, ctx.getString(R.string.default_channel_id))
            .setSmallIcon(R.drawable.thegame)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis())
            .setSound(Uri.parse("android.resource://" + ctx.getPackageName() + "/" + R.raw.annoy1))
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(ctx);

  // HERE is additional code that has nothing to do with the notification and thus is removed

            notify = !TheGameApplication.get_game_list_in_foreground();
            if (notify) {

                Intent acceptIntent = new Intent(ctx, GameActivity.class);
                acceptIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                acceptIntent.setAction(ctx.getString(R.string.broadcast_accept));
                acceptIntent.putExtra(ctx.getString(R.string.extra_notification_id), notificationId);
                acceptIntent.putExtra(ctx.getString(R.string.extra_from_username), fromUsername);
                PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(ctx, 0, acceptIntent, 0);
                mBuilder.addAction(R.mipmap.accept, "Accept", acceptPendingIntent);

                Intent rejectIntent = new Intent(ctx, GameActivity.class);
                rejectIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                rejectIntent.setAction(ctx.getString(R.string.broadcast_reject));
                rejectIntent.putExtra(ctx.getString(R.string.extra_notification_id), notificationId);
                rejectIntent.putExtra(ctx.getString(R.string.extra_from_username), fromUsername);
                PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(ctx, 0, acceptIntent, 0);
                mBuilder.addAction(R.mipmap.reject, "Reject", rejectPendingIntent);

                mBuilder.setContentTitle("Friendship request");
                mBuilder.setContentText(fromUsername + " wants to be your friend. Do you want to ruin your friendship?");
            }

Right now, nothing happens as GameActivity is not a BroadcastReceiver (but it has one). If create the acceptIntent without parameters ( Intent acceptIntent = new Intent(); ) the Receiver inside of GameActivity will receive the action, but the activity will not activate. What do I need to to to create a button that, like the default tap on the notification, will launch that activity?

It seems you're trying to start the activity directly by sending a broadcast from the notification action, but it doesn't work that way.

First you need to send the broadcast to your broadcast receiver (pointing directly to the receiver class). Then, once the receiver receives the broadcast, you create a new intent there to start the activity.

So, supposing your receiver is called 'GameReceiver', you can create your action like this:

Intent acceptIntent = new Intent(ctx, GameReceiver.class);
acceptIntent.setAction(ctx.getString(R.string.broadcast_accept));
acceptIntent.putExtra(ctx.getString(R.string.extra_notification_id),
    notificationId);
acceptIntent.putExtra(ctx.getString(R.string.extra_from_username),
    fromUsername);
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(ctx, 0,
    acceptIntent, 0);
mBuilder.addAction(R.mipmap.accept, "Accept", acceptPendingIntent)

Note that you don't need to set the task flags here. This must done when starting the activity on the receiver.

Then, in your 'GameReceiver', you can start the activity:

Intent intent = new Intent(context, GameActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);

If I understood correctly, your receiver lives as an internal class inside the activity. This can usually lead to confusion (or other kind of errors if the internal class is not static). I recommend to put the receiver in its own file.

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