Android - 如何为GCM消息设置自定义通知声音 [英] Android-How to set Custom notification sound for GCM messages

查看:144
本文介绍了Android - 如何为GCM消息设置自定义通知声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Uri通知=铃声管理器
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
铃声r = RingtoneManager.getRingtone(
context.getApplicationContext(),notification);
if(r!= null){
r.play();
}
Intent i = new Intent(context.getApplicationContext(),
CjStartingActivity.class);

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

解决方案

使用以下代码: - $ / $>

pre $ 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);
铃声铃声= RingtoneManager.getRingtone(getApplicationContext(),uri);
ring.play();
//用于振动手机
((振动器)getApplicationContext()。getSystemService(
Context.VIBRATOR_SERVICE))。vibrate(800);

}


I want to set custom Alert sound for GCM notifications..Here is my current coding..

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);

I have mp3 file in Res folder (Res.raw.mi.mp3).

解决方案

Use following Code:-

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);

}

这篇关于Android - 如何为GCM消息设置自定义通知声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆