【问题标题】:Android Notification doesn't appear安卓通知不出现
【发布时间】:2015-09-07 21:26:38
【问题描述】:

我已经阅读了网站上的很多帖子,我相信为了弹出一个基本的通知,不需要编写很多行代码。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("StartUpActivity", "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_up);


    NotificationManager notiManager = 
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setContentTitle("Hello")
        .setContentText("hello")
        .setSmallIcon(R.drawable.ic_launcher)
        .setAutoCancel(false);
    notiManager.notify(0, mBuilder.build());
    Log.i("here", "here");

}

我也试过 Notification.Builder 也不起作用。我不知道问题出在哪里。由于意图/待处理的意图对于通知是可选的,我只是没有添加代码。 (尽管即使我添加了通知也没有显示的意图)

我还在清单中添加了 VIBRATE 权限,但我不知道它的用法。

我只想知道如何在状态栏上弹出通知。谢谢。

【问题讨论】:

  • 你的“这里”日志会显示吗?
  • 它看起来很干净,不确定尝试将 id 从 0 更改为另一个正数,看看它是否有效。
  • 按原样尝试了您的代码并且工作得很好!
  • 真的吗?这里显示...我的代码有效?
  • 我很伤心。我更改了模拟器并且它可以工作。我以前的一个是 NEXUS 7。另一个不买它的原因......

标签: android android-notifications


【解决方案1】:

振动权限是允许设备振动。您没有使用此权限,您可以将其删除。

你需要在.setAutoCancel(false)之后调用.build();

【讨论】:

    【解决方案2】:

    使用 Android 上的最新版本(22 个目标 sdk 版本)与 sdk 14 兼容,您必须在通知中添加“样式”以显示压缩通知的预览。

    例如:

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(getString(R.string.notifications_title));
    bigTextStyle.bigText(messageText);
    bigTextStyle.setSummaryText(getString(R.string.notification_call_to_action));
    mBuilder.setStyle(bigTextStyle);
    

    之前

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 2017-12-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多