Show Big Style Notification with Share Action by condition of API Level


Function through which you can generate Big Style Notification with Image, Content, Title and Action Option. Here i have provided code for both api level 27+ and below api 26 (support)

STEPS TO CREATE XML FILE

## For Support api 27+ we need to create xml file for file path which stored our file path folder name.
## Create folder "xml" under res-directory.
## Create file named "file_path.xml" under that xml folder.


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="my_images/"/>
</paths>

/**
     * Show bigStyleNotification with big image and share button
     * @param filePath -- file path string
     * @param bitmap -- bitmap image of ticket
     * @param fileName -- only fileName with extension
     */
    private void setBigStyleNotification(String filePath, Bitmap bitmap, String fileName) {

        int num = (int) System.currentTimeMillis();

        // Create the style object with BigPictureStyle subclass.
        NotificationCompat.BigPictureStyle notiStyle = new
                NotificationCompat.BigPictureStyle();
        notiStyle.setBigContentTitle("Title text goes here");
        notiStyle.setSummaryText("Your summary text here");

        // Add the big picture to the style.
        notiStyle.bigPicture(bitmap);

        Uri fileUri = null;
        Intent share = null;
        Intent galleyIntent = null;

        /**
         * Checking for android version and make functionality
         */
        if (Build.VERSION.SDK_INT > 23) { //For API 7

            File file = new File(Environment.getExternalStorageDirectory() + "/" + "my_images" + "/", fileName);
            fileUri = FileProvider.getUriForFile(getContext(), "application package name" + ".fileprovider", file);

            share = new Intent(Intent.ACTION_SEND);
            share.putExtra(Intent.EXTRA_STREAM, fileUri);
            share.setType("image/*");
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            getContext().grantUriPermission(getContext().getPackageName(), fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

            galleyIntent = new Intent(Intent.ACTION_VIEW);
            galleyIntent.setType("image/*");
            galleyIntent.setDataAndType(fileUri, "image/*");
            galleyIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            getContext().grantUriPermission(getContext().getPackageName(), fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        } else { //For Lower API [23]

            //Convert string filePath to File
            File file = new File(filePath);
            //Convert File into URI
            fileUri = Uri.parse("file://" + file);

            /**
             * Prepare share intent
             */
            share = new Intent(Intent.ACTION_SEND);
            share.putExtra(Intent.EXTRA_STREAM, fileUri);
            share.setType("image/*");
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            getContext().grantUriPermission(getContext().getPackageName(), fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

            /**
             * Prepare gallery intent
             */
            galleyIntent = new Intent(Intent.ACTION_VIEW);
            galleyIntent.setType("image/*");
            galleyIntent.setDataAndType(fileUri, "image/*");
            galleyIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            getContext().grantUriPermission(getContext().getPackageName(), fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }

        /**
         * Prepare PendingIntent for Share
         */
        PendingIntent shareResult = PendingIntent.getActivity(getContext(),
                num, share, 0);

        /**
         * Prepare PendingIntent for Gallery
         */
        PendingIntent galleryResult = PendingIntent.getActivity(getContext(),
                num, galleyIntent, 0);


        Notification myNotification = new NotificationCompat.Builder(getContext())
                .setSmallIcon(R.drawable.ic_receipt_white_24dp)
                .setAutoCancel(true)
                .setLargeIcon(bitmap)
                .setContentIntent(galleryResult)
                .setContentTitle("Content title goes here")
                .setContentText("Context text here")
                .addAction(R.drawable.ic_share, "Share", shareResult)
                .setAutoCancel(true)
                .setStyle(notiStyle).build();

        NotificationManager notificationManager =
                (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(num, myNotification);

    }

Output

Comments