首页 > 解决方案 > 来自通知点击的用户

问题描述

如果用户来自通知点击,我想触发 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());

标签: javaandroidpush-notificationnotifications

解决方案


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

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

如果 Home_Base 中收到的操作是 On_NOTIFICATION_CLICK,则它是通知单击操作。


推荐阅读