Question

I use Phonegap's localNotification Plugin for Android to show notifications on specific dates.

I use Cordova [2.2] & I used cordova's upgrading tutorial to modify the plugin.

The notification is displayed, but when I click on it, the application doesn't open and the notification isn't cleared.

How can I fix this?

Was it helpful?

Solution

In AlarmReceiver.java, around line 70, you will see the following lines of code:

    // Construct the notification and notificationManager objects
    final NotificationManager notificationMgr = (NotificationManager) systemService;
    final Notification notification = new Notification(R.drawable.ic_launcher, tickerText,
            System.currentTimeMillis());
    final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.vibrate = new long[] { 0, 100, 200, 300 };
    notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent);

Add the appropriate lines to match the following:

    // Construct the notification and notificationManager objects
    final NotificationManager notificationMgr = (NotificationManager) systemService;
    final Notification notification = new Notification(R.drawable.ic_launcher, tickerText,
            System.currentTimeMillis());
    Intent notificationIntent = new Intent(context, CLASS_TO_OPEN.class);
    final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.vibrate = new long[] { 0, 100, 200, 300 };
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, notificationTitle, notificationSubText, contentIntent);

where CLASS_TO_OPEN is the name of the class you wish to open when the notification is pressed.

EDIT:
To clarify, in order to have the notification open an activity when it is press, you need to associate this activity with the notification object. This is done by creating an Intent, specifying the activity to open (as in NAME_OF_ACTIVITY.class) as the second parameter, and passing this Intent to a PendingIntent as the third parameter. This, in turn, is passed to the notification object via the setLatestEventInfo method.

In the code snippet above this is all done for you with the exception of specifying the activity to open, as this is going to be specific to your project. Unless you have added additional activities, a PhoneGap/Cordova project contains one activity, namely the one that opens the Cordova WebView. If you do not know or remember the name of this activity in your project, you can find it in the Package Explorer (in Eclipse) by following:

src>NAME_OF_YOUR_PACKAGE>NameOfActivity.java

To be sure this is the name of the class, open the java file with a text editor and you will see NAME_OF_ACTIVITY extends DroidGap. Replace CLASS_TO_OPEN in the above snippet with the name of your activity (must include .class file extension).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top