当GCMIntentService的onMessage触发时如何播放声音文件?

3

我在我的应用程序中使用GCM服务。我想在onMessage函数中播放一个小的mp3文件或者哔哔声。

我的GCMIntentService是从GCMBaseIntentService扩展而来的。

请问有人可以分享代码或建议我如何实现这个功能吗?

谢谢

1个回答

6
你可以使用以下方法:

Notification notification = new Notification(icon, title, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND; // To play default notification sound
notification.sound = Uri.parse("PATH_TO_YOUR_SOUND_FILE"); // to play custom notification sound
NotificationManager nm = (NotificationManager)   getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(System.currentTimeMillis(), notification);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接