Android 如何在用户单击';安卓系统中的通知是什么?

Android 如何在用户单击';安卓系统中的通知是什么?,android,android-notifications,Android,Android Notifications,我在android中向用户显示文件下载通知 现在我想在用户单击该通知时打开该文件 我已经使开放文件方法工作正常。我只是不知道如何设置click listener on notification来调用这个方法 我的通知创建者方法 public void displayFileDownloadNotification(Context context, String fileName) { new CommonMethod().createNotificationChannel

我在android中向用户显示文件下载通知

现在我想在用户单击该通知时打开该文件

我已经使开放文件方法工作正常。我只是不知道如何设置click listener on notification来调用这个方法

我的通知创建者方法

public void displayFileDownloadNotification(Context context, String fileName)
    {
        new CommonMethod().createNotificationChannel(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
        builder.setSmallIcon(R.mipmap.cghs_launcher_logo);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.cghs_launcher_logo));
        builder.setContentTitle("CGHS");
        builder.setContentText("Downloaded "+fileName);
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(NOTIFICATION_ID++, builder.build());
    }

    public void createNotificationChannel(Context context)
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            CharSequence name = "Attachment Download Notification";
            String description = "Simple Description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
public static void openDownloadedFile(String fileName, Context context)
    {
        try{
            String path = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/Download/" + fileName);
            Log.e("File Path=",path);
            File file = new File(path);
            if (file.exists()) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Intent target = Intent.createChooser(intent, "Open File");
                try {
                    context.startActivity(target);
                } catch (Exception e)
                {
                    Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
                }
            } else {
                Log.e("File = ", "File does not exits");
            }
        } catch (Exception e)
        {
            Log.e("Error ",e.getMessage());
            Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
        }
    }
和我的文件打开方法

public void displayFileDownloadNotification(Context context, String fileName)
    {
        new CommonMethod().createNotificationChannel(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
        builder.setSmallIcon(R.mipmap.cghs_launcher_logo);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.cghs_launcher_logo));
        builder.setContentTitle("CGHS");
        builder.setContentText("Downloaded "+fileName);
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(NOTIFICATION_ID++, builder.build());
    }

    public void createNotificationChannel(Context context)
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            CharSequence name = "Attachment Download Notification";
            String description = "Simple Description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
public static void openDownloadedFile(String fileName, Context context)
    {
        try{
            String path = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/Download/" + fileName);
            Log.e("File Path=",path);
            File file = new File(path);
            if (file.exists()) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Intent target = Intent.createChooser(intent, "Open File");
                try {
                    context.startActivity(target);
                } catch (Exception e)
                {
                    Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
                }
            } else {
                Log.e("File = ", "File does not exits");
            }
        } catch (Exception e)
        {
            Log.e("Error ",e.getMessage());
            Toast.makeText(context, "No Application available to view pdf", Toast.LENGTH_LONG).show();
        }
    }
现在,当单击通知时,我如何调用这个openDownloadedFile

  • 创建新活动 在OnCreate中调用
    openDownloadedFile

  • 编辑您的通知:

  • Intent Intent=新的Intent(这个,NewActivity.class)
    PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,PendingEvent.FLAG_ONE_SHOT)
    builder.setContentIntent(挂起内容)


    感谢它的工作,我只需要做一些小的调整“FLAG\u UPDATE\u CURRENT”而不是“FLAG\u ONE\u SHOT”,因为我同时有多个通知。Intent Intent=new Intent(context,OpenNotification.class);intent.putExtra(“文件名”,文件名);intent.setAction(Long.toString(System.currentTimeMillis());PendingEvent PendingEvent=PendingEvent.getActivity(上下文,0,意图,PendingEvent.FLAG_UPDATE_CURRENT);builder.setContentIntent(挂起内容);