如何在状态栏中显示通知



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

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

我现在如何在状态/通知栏中显示它以及声音?

在安卓开发者网站上有一些很好的文档并查看通知管理器文档的

来了,一些代码...:

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()方法。

我现在如何在状态/通知栏中显示它以及声音?

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

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

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

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

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

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

myNotificationManager.notify(myNotificationId, n);

最新更新