以下代码已确认可在运行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
复制到我的项目中。我无法想出调试此问题的方法。我不确定我是否还有什么遗漏。任何解决此问题的建议都表示赞赏。
答案 0 :(得分:9)
正如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();
答案 1 :(得分:0)
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();
答案 2 :(得分:0)
当我尝试上述解决方案时,它崩溃了应用程序,只是声明了空的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);