2012-12-10 77 views

回答

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作爲第三個參數傳遞給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文件擴展名)。

+0

非常感謝,當我點擊它時,通知現在從狀態欄中消失了,但我什麼也沒打開。 –