Android GCM PushNotification - 添加在应用程序中添加自定义声音文件

Kal*_*har 8 android uri push-notification google-cloud-messaging

我成功地获得了GCM推送通知.现在我想添加自定义声音文件而不是默认声音.我曾尝试与乌里

文件:///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);



}
Run Code Online (Sandbox Code Playgroud)

Gir*_*hai 15

要添加自定义声音,请添加此项

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);
Run Code Online (Sandbox Code Playgroud)

即在您的代码更改

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)