【问题标题】:Android notifications from a BroadcastReceiver not showing来自 BroadcastReceiver 的 Android 通知未显示
【发布时间】:2013-11-22 20:15:09
【问题描述】:

我想创建一个通知(来自 BroadcastReceiver - 如果有影响的话)

我不知道为什么,但它只是没有出现!

private void showNotification(Context context, String text) {

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(text)
                        .setAutoCancel(true);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(0, mBuilder.build());

    }

我试过this(和this

【问题讨论】:

  • 显示你的整个 BroadcastReceiver 类和调用它的类。
  • 添加 Log.d(TAG, "message");行以确保您实际访问此代码(日志消息将显示在 Logcat 中)

标签: java android android-notifications


【解决方案1】:

从 BroadcastReceiver 创建一个通知 - 这很重要! 你应该在你的代码中重写这个字符串:

NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

【讨论】:

  • 我应该早点说:BroadcastReceiver 在一个服务类中,所以这没有什么区别
【解决方案2】:

问题可能如下:

-1:将方法 showNotification 添加到广播接收器不调用它的位置。

-2: 你没有注册广播接收器

-3:替换这个

mNotificationManager.notify(0, mBuilder.build());

mNotificationManager.notify(1, mBuilder.build());

如果第 3 点不起作用,请附上您的整个广播接收器代码,以及您调用它的位置。

【讨论】:

    【解决方案3】:

    感谢所有答案,但我意外地解决了。 我不知道哪一点真正解决了它,但我试过这个: 1.清洁 2. 重建 3.关闭安卓工作室 4.重启 5.重启android studio

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      相关资源
      最近更新 更多