2011-10-23 73 views
6

我有一個擴展BroadcastReceiver的類,每當新的Wifi掃描結果可用時(接收方在Scan_Results廣播中作爲意圖過濾器註冊到接收方中)都會被調用。來自BroadcastReceiver的Android展示通知

從這個類,我想能夠顯示一個通知給用戶。目前,我將在我的廣播意圖類的onReceive方法中作爲參數接收的上下文傳遞給另一個類的「show notification」方法。

當它到達線路:

myNotificationManager.notify(notificationId, notification); 

它失敗,出現以下異常:

java.lang.IllegalArgumentException: contentView required: pkg=com.mumfordmedia.trackify id=2131034122 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0) 

任何想法,爲什麼發生這種情況?我所能想到的是,因爲我從onReceive參數獲得的上下文不是......因爲沒有更好的短語,「適合這份工作」......

任何想法? 謝謝, 最大。

+1

也許這http://stackoverflow.com/questions/2826786/pendingintents-in-notifications會有所幫助。無論如何,向我們展示更多代碼,以便我們提供幫助。 – Jong

+2

請向我們展示一些您的代碼,以便我們更好地瞭解正在發生的事情。 –

+0

順便說一句,歡迎來到Stackoverflow!如果你的迴應有幫助,請認真投票。如果回覆成功回答您的問題,請點擊旁邊的綠色複選標記以接受答案。 –

回答

0

從我所知道的,當您創建通知傳遞給通知管理器時,您並未給它顯示內容視圖。查看您實際創建通知的行,看看您是否實際上爲通知顯示了一個視圖。

+0

我從應用程序中的其他地方複製並粘貼了通知腳本。沒有可用的內容視圖,因爲它是從擴展沒有佈局的BroadcastReceiver的類中激活的。爲什麼需要內容視圖?我會在幾個小時內回顧文檔......感謝回覆btw –

2

不知道究竟爲什麼它不是以前的工作,但這裏是我得到了它的工作代碼:

聲明以下任何方法之外:

int YOURAPP_NOTIFICATION_ID = 1234567890; 
NotificationManager mNotificationManager; 

然後在的onReceive方法請撥打以下:

mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
showNotification(context, R.drawable.icon, "short", false); 

然後聲明如下方法:

private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) { 
     // This is who should be launched if the user selects our notification. 
     Intent contentIntent = new Intent(); 

     // choose the ticker text 
     String tickerText = "ticket text"; 

     Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis()); 

     PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, 0); 

     n.setLatestEventInfo(context, "1", "2", appIntent); 

     mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n); 
    } 
+0

這段代碼爲我運行。謝謝:) – IRvanFauziE

2

您必須調用Notification.setLatestEventInfo()。

1

使用此代碼的通知

Intent intent = new Intent(this, MusicDroid.class); 
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); 
notification.setLatestEventInfo(this, "This is the title", 
    "This is the text", activity); 
notification.number += 1; 

nm.notify(NOTIFY_ID, notification); 
14

內容查看一起是在點擊通知時所需要的視圖。下面的代碼工作正常,setLatestEventInfo()是必需的方法。

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, 
      "Hello from service", System.currentTimeMillis()); 
    Intent intent = new Intent(this, MainActivity.class); 
    notification.setLatestEventInfo(this, "contentTitle", "contentText", 
      PendingIntent.getActivity(this, 1, intent, 0)); 
    manager.notify(111, notification); 
+1

+1:使用setLatestEventInfo()導致錯誤消失。這有點煩人,需要多少行代碼來獲得這樣一個簡單的通知顯示... – ArtOfWarfare

0

對於那些誰使用NotificationCompat,下面的代碼將工作:

NotificationCompat.Builder n = new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.icon).setContentText("Notify Title").setContentText("Sample Text"); 
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    Intent i = new Intent(this,MainActivity.class); 
    PendingIntent ac = PendingIntent.getActivity(this, 0, i, 0); 
    n.setContentIntent(ac); 
    nm.notify(12222, n.build());