我使用Phonegap's localNotification Plugin for Android在特定日期显示通知。清除Android的本地通知cordova plugin
我使用科尔多瓦[2.2] &我用cordova的upgrading tutorial来修改插件。
显示通知,但是当我点击它时,应用程序不会打开,通知也不会被清除。
我该如何解决这个问题?
我使用Phonegap's localNotification Plugin for Android在特定日期显示通知。清除Android的本地通知cordova plugin
我使用科尔多瓦[2.2] &我用cordova的upgrading tutorial来修改插件。
显示通知,但是当我点击它时,应用程序不会打开,通知也不会被清除。
我该如何解决这个问题?
在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
作为第三个参数传递给PendingIntent
来完成的。这反过来又通过setLatestEventInfo
方法传递给通知对象。
在上面的代码片段中,除了指定要打开的活动之外,这一切都已完成,因为这将特定于您的项目。除非添加了其他活动,否则PhoneGap/Cordova项目包含一项活动,即打开Cordova WebView的活动。如果你不知道或者不记得在你的项目这个活动的名称,您可以通过以下发现它在Package Explorer(在Eclipse):
来源> NAME_OF_YOUR_PACKAGE> NameOfActivity.java
要确定这是该类的名称,请使用文本编辑器打开java文件,您将看到NAME_OF_ACTIVITY extends DroidGap
。将上面代码片段中的CLASS_TO_OPEN替换为您的活动名称(必须包含.class文件扩展名)。
非常感谢,当我点击它时,通知现在从状态栏中消失了,但我什么也没打开。 –