[英]Android : Notification not working on 2.3.6 (Samsung galaxy y)
以下代碼已經確認可以在運行HONEYCOMB +的設備上正常運行。 但是在三星Galaxy Y上沒有發出任何通知。
String tickerText = userString + " Download Queued";
Notification notification = new NotificationCompat.Builder(this).setAutoCancel(true)
.setContentTitle(userString)
.setContentText("Queued")
.setSmallIcon(R.drawable.stat_sys_download_done)
.setWhen(System.currentTimeMillis())
.setTicker(tickerText)
.build();
if(DBG_ENABLE) {
LogUtils.logD(TAG_LOG, "Posting queue notification : " + 0);
}
NotificationManager notificationManager =
(NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
注意 :
stat_sys_download_done
復制到我的項目中。 我無法想到調試此問題的方法。 我不確定我是否還有什么遺漏。 任何建議來解決這個問題表示贊賞。
正如CommonsWare建議的那樣,我在2.3模擬器上運行應用程序並且崩潰了。 原因是沒有設置ContentIntent。 GingerBread期待ContentIntent 。 所以我添加了一個假的待定意圖,如:
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this).setAutoCancel(true)
.setContentTitle(userString)
.setContentText("Queued")
.setContentIntent(pi)
.setSmallIcon(R.drawable.stat_sys_download_done)
.setWhen(System.currentTimeMillis())
.setTicker(tickerText)
.build();
y0u也可以這樣做。它適用於2.3和2.3+
Notification notification = new NotificationCompat.Builder(this)
.setTicker("new notification")
.setContentTitle("title")
.setContentText("hello").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_media_play, "play",pendingIntent).build();
當我嘗試上面的解決方案時,它崩潰了應用程序,只是聲明了空的PendingIntent,如圖所示。 確定我有一個范圍問題,因為我在BroadcastListener中編寫此通知,我將待處理的意圖移動到偵聽器的onReceive方法,並使用傳入的上下文和意圖而不是dummys。 這在我的2.3.6 Galaxy手機中完美運行。 這是我的代碼:
BroadcastReceiver sitInReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String target = intent.getStringExtra("target");
....
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notifB.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int nmId=1;
// mId allows you to rewrite the same the notification or make a different one.
mNotificationManager.notify(nmId, notifB.build());
}
請注意,我在偵聽器外部聲明了構建器notifB,其中聲明了所有非動態數據:
NotificationCompat.Builder notifB = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.