来自通知的用户点击

时间:2018-11-25 15:01:41

标签: java android push-notification notifications

如果用户来自“通知”点击,我想触发Toast。

但是我不知道如何知道用户是否来自通知点击。 知道我设置了这样的通知:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;

        NotificationChannel channel =
                new NotificationChannel(notification_channel, notification_channel, importance);

        String description = "A channel which shows notifications about the app";
        channel.setDescription(description);

        channel.setLightColor(Color.MAGENTA);

        NotificationManager notificationManager = (NotificationManager) getApplicationContext().
                getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }

    String notificationTitle = "Title";
    String notificationText = "Notification Text";

    Intent intent_not = new Intent(getApplicationContext(), Home_Base.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent_not, FLAG_UPDATE_CURRENT);

    //build the notification
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(getApplicationContext(), notification_channel)
                    .setSmallIcon(R.drawable.fiamma)
                    .setContentTitle(notificationTitle)
                    .setContentText(notificationText)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent);

    //trigger the notification
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(getApplicationContext());

    notificationManager.notify(123, notificationBuilder.build());

1 个答案:

答案 0 :(得分:0)

尝试使用intent_not.setAction()设置操作(例如On_NOTIFICATION_CLICK)

在Home_Base中使用getIntent()。getAction(),尝试从收到的意图中获取操作。

如果在Home_Base中收到的操作为On_NOTIFICATION_CLICK,则其为通知Click操作。