2

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?

4

1 回答 1

4

在 AlarmReceiver.java 中,大约第 70 行,您将看到以下代码行:

    // 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);

添加适当的行以匹配以下内容:

    // 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);

其中 CLASS_TO_OPEN 是您希望在按下通知时打开的类的名称。

编辑:
为了澄清,为了让通知在按下时打开一个活动,您需要将此活动与通知对象相关联。这是通过创建一个Intent,指定要打开的活动(如在 NAME_OF_ACTIVITY.class 中)作为第二个参数,并将其作为第三个参数传递Intent给 aPendingIntent来完成的。这又通过该setLatestEventInfo方法传递给通知对象。

在上面的代码片段中,除了指定要打开的活动之外,这一切都是为您完成的,因为这将特定于您的项目。除非您添加了其他活动,否则 PhoneGap/Cordova 项目包含一个活动,即打开 Cordova WebView 的活动。如果您不知道或不记得项目中此活动的名称,您可以通过以下方式在 Package Explorer(在 Eclipse 中)中找到它:

src>NAME_OF_YOUR_PACKAGE>NameOfActivity.java

为确保这是类的名称,请使用文本编辑器打开 java 文件,您将看到NAME_OF_ACTIVITY extends DroidGap. 将上述代码段中的 CLASS_TO_OPEN 替换为您的活动名称(必须包含 .class 文件扩展名)。

于 2012-12-26T14:26:35.867 回答