【问题标题】:Can I change push notification sound when app is closed?应用程序关闭时我可以更改推送通知声音吗?
【发布时间】:2014-07-01 18:27:50
【问题描述】:
当我通过 GCM 向 Android 手机发送通知时,我会发送一个声音名称,如果应用程序正在运行,或者当用户单击通知时播放该名称。
我的问题是我可以更改通知的声音吗?不是当用户单击通知时,而是当通知在手机中弹出时。我知道有可能,Yo app 在弹出通知推送时播放“YO”音。
对不起我的英语:s,谢谢你的帮助!
【问题讨论】:
标签:
android
audio
notifications
push-notification
google-cloud-messaging
【解决方案1】:
当您构建通知时,您可以使用setSound(uri) 为通知设置声音。
public NotificationCompat.Builder setSound (Uri sound)
Set the sound to play. It will play on the default stream.
或者您可以使用setDefaults(Notification.DEFAULT_SOUND) 播放默认声音。
例如:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_SOUND)
.setTicker (text)
.setSmallIcon(R.drawable.icon)
.setContentText (text)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(text))
.setAutoCancel(true).setNumber (4)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
据我所知,显示通知时会播放此声音(尽管我没有检查过)。但如果没有,您可以在调用 mNotificationManager.notify 之前播放声音,而不考虑显示通知的代码中的通知(在您的广播接收器或意图服务中)。