简体   繁体   中英

Cordova plugin (java) notification doesn't go to app - android

I'm not an android developer, but I need to make an app, so since I know some HTML/JavaScript/CSS I decided to use PhoneGap (Cordova). I use a plugin called download manager ( github ) which downloads files and display a progress notification. Everything works, but I want to bring me back to the main activity of my app once I click on the notification, but that doesn't happen.

This is the file responsible for the download and notification, the involved code is below:

intent = new Intent();
intent.putExtra("cancel_download", 1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pend = PendingIntent.getActivity(cordova.getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

mNotifyManager = (NotificationManager) cordova.getActivity().getSystemService(Activity.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(cordova.getActivity())
    .setSmallIcon(android.R.drawable.stat_sys_download)
    .setContentTitle(notificationTitle)
    /*.setSubText("Tap to CANCEL")*/
    .setTicker(ticker)
    .setContentIntent(pend)
    .setContentText("0% - " + fileName);

mNotificationId = new Random().nextInt(10000);
...

...
//While(downloading)
if(useNotificationBar) {
    mBuilder.setProgress(100, newProgress, false);
    mBuilder.setContentText(step + "% - " + fileName);
    mBuilder.setContentIntent(pend);
    mNotifyManager.notify(mNotificationId, mBuilder.build());
}

I can't make it work when I click the notification nothing happens. What is wrong? Sorry for bad English.

Change

pend = PendingIntent.getActivity(cordova.getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

to

pend = PendingIntent.getActivity(cordova.getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

Hope this works.

Try this :

 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                PendingIntent intent = PendingIntent.getActivity(context, 0,
                        notificationIntent, 0);

Substitute it with your variables. Hope it works

Seems like the library author intended clicking on the notification to cancel the download. Anyway,the original intent wrapped the with the PendingIntent does not specify an Activity to start.

you should change:

intent = new Intent ();
intent.putExtra("cancel_download", 1);
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

to:

intent = new Intent ();
intent.setComponent(cordova.getActivity.getComponentName());
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM