代码之家  ›  专栏  ›  技术社区  ›  GMX

来自通知单击的用户

  •  0
  • GMX  · 技术社区  · 6 年前

    如果用户来自通知单击,我想触发一个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 回复  |  直到 6 年前
        1
  •  0
  •   jaede    6 年前

    尝试使用intent设置操作(例如,单击时)_not.set动作()

    在Home\u Base中,使用getIntent().getAction(),尝试从接收到的intent获取操作。

    如果在Home\u Base中收到的操作是基于\u NOTIFICATION\u CLICK,则它是一个NOTIFICATION CLICK操作。