Android-如何为GCM消息设置自定义通知声音

时间:2014-09-16 06:10:05

标签: android google-cloud-messaging

我想为GCM通知设置自定义警报声。这是我目前的编码..

Uri notification = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(
                context.getApplicationContext(), notification);
        if(r!= null){
         r.play();
        }
Intent i = new Intent(context.getApplicationContext(),
                CjStartingActivity.class);

我在Res文件夹(Res.raw.mi.mp3)中有mp3文件。

1 个答案:

答案 0 :(得分:3)

使用以下代码: -

private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, LoginActivity.class);
    intent.putExtra(IAppConstants.IS_NOTIFICATION, true);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("App Name")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg).setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    Uri uri =Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.sound);
     Ringtone ring = RingtoneManager.getRingtone(getApplicationContext(), uri);
     ring.play();
    // for vibrating phone
    ((Vibrator) getApplicationContext().getSystemService(
            Context.VIBRATOR_SERVICE)).vibrate(800);

}