Android GCM PushNotification - 在应用程序中添加添加自定义声音文件
Posted
技术标签:
【中文标题】Android GCM PushNotification - 在应用程序中添加添加自定义声音文件【英文标题】:Android GCM PushNotification - Add add custom sound file in app 【发布时间】:2014-07-14 07:31:39 【问题描述】:我已成功收到 GCM 推送通知。现在我想添加自定义声音文件而不是默认声音。我已经尝试使用来自
的 Urifile:///res/raw/pop.mp3
在
Notification.DEFAULT_SOUND;
但没有成功。如果您有更好的解决方案,请分享。
我的 GCMIntentService.java 方法代码如下 -
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message)
System.out.println("Called generateNotification>>>>>>>>>>>>>"+message);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(title)
.setStyle(
new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message);
Intent notificationIntent = new Intent(context,
SplashActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(intent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
【问题讨论】:
【参考方案1】:要添加自定义声音添加这个
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);
即在您的代码更改中
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
到
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;
【讨论】:
以上是关于Android GCM PushNotification - 在应用程序中添加添加自定义声音文件的主要内容,如果未能解决你的问题,请参考以下文章
Android:GCM 错误 - 字符串/gcm_defaultSendorid 错误:重复资源错误
将com.google.android.gcm.GCMBaseIntentService升级到com.google.android.gms.gcm.GoogleCloudMessaging
Android - GCM 在打开应用程序时收到来自 gcm 的通知
获取通知密钥错误 401 gcm https://android.googleapis.com/gcm/googlenotification