清除Android的本地通知cordova插件

2023-12-10

I use 适用于 Android 的 Phonegap 本地通知插件显示特定日期的通知。

我使用 Cordova [2.2] & 我使用 cordova 的升级教程修改插件。

显示通知,但当我单击它时,应用程序未打开且通知未清除。

我怎样才能解决这个问题?


在 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 是您希望在按下通知时打开的类的名称。

EDIT:
澄清一下,为了让通知在按下时打开一个 Activity,您需要将该 Activity 与通知对象关联起来。这是通过创建一个Intent,指定要打开的活动(如 NAME_OF_ACTIVITY.class 中所示)作为第二个参数,并传递此参数Intent to a PendingIntent作为第三个参数。这又通过以下方式传递给通知对象:setLatestEventInfo method.

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

src>NAME_OF_YOUR_PACKAGE>NameOfActivity.java

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

清除Android的本地通知cordova插件 的相关文章

随机推荐