display progress bar in cordova local notification

2019-03-29 12:58发布

i use this great plugin https://github.com/katzer/cordova-plugin-local-notifications to implement local notification when downloading a file. i don't find how to display a progress bar in the notification like in native one http://javatechig.com/wp-content/uploads/2014/05/Displaying-Progress-Notification-in-Android-Example.png can you help?

1条回答
做自己的国王
2楼-- · 2019-03-29 13:53

Use plugin cordova-file-Transfer and make the following changes:

You can change the plugin this way for android platform.

Create class FileProgressBarTask with bellow code:

package org.apache.cordova.filetransfer;

import android.app.NotificationManager;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

class FileProgressBarTask extends AsyncTask<Void, Integer, Integer> {

    private NotificationCompat.Builder mBuilder;
    private NotificationManager mNotificationManager;
    int id = 0;
    int progress = 0;

    FileProgressBarTask(NotificationCompat.Builder mBuilder, NotificationManager mNotificationManager, int id){

        Log.d("TAG", "Progress Bar");

        this.mBuilder = mBuilder;
        this.mNotificationManager = mNotificationManager;
        this.id = id;

        super.execute();
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();

        mBuilder.setProgress(150, 0, false);
        mNotificationManager.notify(id, mBuilder.build());
    }

    @Override
    protected void onProgressUpdate(Integer... values){
        mBuilder.setProgress(150, values[0], false);
        mNotificationManager.notify(id, mBuilder.build());
        super.onProgressUpdate(values);
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Integer result){
        super.onPostExecute(result);
        mBuilder.setContentText("Download Concluído");

        mBuilder.setProgress(0, 0, false);
        mNotificationManager.notify(id, mBuilder.build());
    }
}

Change class FileTransfer with bellow code:

import android.content.res.Resources;
import android.content.Context;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;

On the line ~700 in method download on class FileTransfer:

Context contextApplication = cordova.getActivity().getApplicationContext();
Resources resources = contextApplication.getResources();
String pkgName = contextApplication.getPackageName();

int resId = resources.getIdentifier("ic_action_download", "drawable", pkgName);

mNotificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(cordova.getActivity());
mBuilder.setContentTitle("Download File")
        .setContentText("Progress")
        .setSmallIcon(resId);

final FileProgressBarTask progressBarTask = new FileProgressBarTask(mBuilder, mNotificationManager, id);

Find the block code on the method download that contains: while and progress.setLoaded(inputStream.getTotalRawBytesRead()); on method download, insert bellow code:

long lng = Math.abs((progress.getLoaded() / 100) / 100);
progressBarTask.onProgressUpdate(Integer.parseInt(String.valueOf(lng)));

Based on:

查看更多
登录 后发表回答