【问题标题】:Android : Notification not working on 2.3.6 (Samsung galaxy y)Android : 通知在 2.3.6 (Samsung Galaxy y) 上不起作用
【发布时间】:2013-02-07 07:56:02
【问题描述】:

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

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

【问题讨论】:

  • 看起来不错。在 2.3 模拟器上试一试。

标签: android notifications fragmentation


【解决方案1】:

按照 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();

【讨论】:

  • 谢谢。我不知道 Gingerbread 和 ContentIntent
【解决方案2】:

你也可以这样做。它适用于 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();

【讨论】:

    【解决方案3】:

    当我尝试上述解决方案时,它使应用程序崩溃,只是声明了空的 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);
    

    【讨论】:

      猜你喜欢
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多