3

已确认以下代码可在运行 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_doneandroid sdk 中的可绘制对象复制到我的项目中。

我想不出调试这个问题的方法。我不确定我是否缺少任何东西。任何解决此问题的建议表示赞赏。

4

3 回答 3

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();
于 2013-02-21T21:30:49.057 回答
0

你也可以这样做。它适用于 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();
于 2014-02-04T09:38:44.233 回答
0

当我尝试上述解决方案时,它使应用程序崩溃,只是声明了空的 PendingIntent,如图所示。确定我遇到了范围问题,因为我在 BroadcastListener 中编写此通知,我将挂起的意图移动到侦听器的 onReceive 方法中,并使用传入的上下文和意图而不是虚拟对象。这在我的 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);
于 2014-04-30T14:17:33.840 回答