【问题标题】:How to show notification in status bar?如何在状态栏中显示通知?
【发布时间】:2012-12-16 22:06:41
【问题描述】:

所以我在我的活动中创建了这个通知

Notification n = new Notification.Builder(getApplicationContext())
    .setContentTitle("New mail from " + sender)
    .setContentText(subject)
    .setSmallIcon(R.drawable.notification)
    .build();

我现在如何将它与声音一起显示在状态/通知栏中?

【问题讨论】:

    标签: android android-notification-bar


    【解决方案1】:

    有一些不错的documentation at android developer site 并查看NotificationManager 文档

    给你,一些代码...:

    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, n);
    

    请注意,将您的意图添加到通知中也是一个好主意...

    mBuilder.setContentIntent(resultPendingIntent);
    

    要添加声音,您可以使用Notification.Builder.setSound() 方法。

    【讨论】:

    • 是否需要添加intent?我的意思是当我开始活动时它会开始吗?
    • 它的 mId 是什么,我的意思是我应该为它分配哪个值,因为它以这种方式显示错误?
    • 我从来没有尝试过没有它,但如果你不添加它,我认为它不会启动活动。
    • 你可以分配任何int,你可以使用这个int稍后更新通知
    • 我还有一个问题,这似乎与 11 及更高版本兼容,我需要它在 2.2 上工作。是否有另一种方法可以通过声音通知用户
    【解决方案2】:

    我现在如何将它与声音一起显示在状态/通知栏中?

    使用Notification.Builder.setSound() 方法。

    你可以像这样获取默认铃声:

    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    

    然后将其设置为通知声音:

    Notification.Builder(getApplicationContext()).setSound(uri);
    

    然后在您构建通知后,您可以使用以下命令启动它:

    myNotificationManager.notify(myNotificationId, n);
    

    【讨论】:

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