点击通知

时间:2018-05-27 12:03:11

标签: java android

目前我正在为我的应用构建一些通知。一个用例是你得到了一个朋友请求,我在通知中添加了两个按钮,如果需要,可以接受或拒绝来自通知的请求。这是我的代码:

    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?");
            }

现在,没有任何事情发生,因为GameActivity不是BroadcastReceiver(但它有一个)。如果创建不带参数的acceptIntent(Intent acceptIntent = new Intent();),GameActivity内的Receiver将收到操作,但活动将不会激活。我需要创建一个按钮,就像通知中的默认点击一样,会启动该活动吗?

1 个答案:

答案 0 :(得分:1)

您似乎试图通过从通知操作发送广播来直接启动活动,但它不会那样工作。

首先,您需要将广播发送到广播接收器(直接指向接收器类)。然后,一旦接收者收到广播,您就可以在那里创建一个新意图来开始活动。

因此,假设你的接收器被称为'GameReceiver',你可以像这样创建你的动作:

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)

请注意,您无需在此处设置任务标志。这必须在接收器上启动活动时完成。

然后,在“GameReceiver”中,您可以开始活动:

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

如果我理解正确,你的接收者就会在活动中作为一个内部阶级。这通常会导致混淆(或者如果内部类不是静态的,则会出现其他类型的错误)。我建议将接收器放在自己的文件中。