2010-07-03 61 views
2

通過我的通知系統,發送通知時,我希望能夠點擊通知並將其發送給我的應用程序。但是,用我目前的代碼,它並沒有這樣做。我該如何解決這個問題?點擊通知 - 發送到應用程序

public void causeNotification(CharSequence contentText, CharSequence tickerText) { 
    Log.d(TAG, "Sending notification - \"" + contentText + "\""); 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
    int icon = R.drawable.neoseeker_swoosh; 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, when); 
    notification.defaults |= Notification.DEFAULT_ALL; 
    notification.defaults |= Notification.FLAG_AUTO_CANCEL; 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "Neoseeker"; 
    Intent notificationIntent = new Intent(); 
    PendingIntent contentIntent = PendingIntent.getActivity(Neoseeker.this, 0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

    mNotificationManager.notify(Neoseeker.NOTIFICATION_ID, notification); 
} 

回答

3

步驟#1:永遠不要打電話getApplicationContext()Neoseeker.this顯然是Context - 使用它。

步驟#2:notificationIntent是空的Intent。這不會開展任何活動。您需要創建一個啓動活動的Intent,與您使用startActivity()時使用的任何Intent非常相似。

這是我的一本書中的sample project,顯示了通知的使用。

+0

工作就像它,謝謝隊友! – Chiggins 2010-07-04 16:47:43

相關問題