FCM自定义通知声音 [英] FCM Custom notification sound

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

问题描述

我的Andriod应用程序具有Firebase消息传递功能.我正在使用Firebase发送推送通知.我想将默认通知声音更改为自定义声音.我该怎么做 ?

I have Firebase messaging with my andriod application. I am using Firebase to send push notifications. I want to change the default notification sound to a custom one. how can i do it ?

    Uri defaultSoundUri = 
    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.mipmap.ic_notif)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setCustomBigContentView(remoteViews)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))
            ;


    NotificationManager notificationManager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(Integer.parseInt(id) /* ID of notification */, notificationBuilder.build());
}

推荐答案

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

使用上面的代码从资源中添加自定义声音.

Use above code to add custom sound from resources.

如果我们使用Notification类,则可以使用上面的代码.

The above code can be used if we are using Notification class.

Notification notification = new Notification(icon, tickerText, when);

在使用NotificationBuilder时,请使用以下代码.

As you are using NotificationBuilder, use the below code.

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setLargeIcon(image)/*Notification icon image*/
        .setSmallIcon(R.mipmap.ic_notif)
        .setContentTitle(title)
        .setAutoCancel(true)
        .setSound(sound)
        .setContentIntent(pendingIntent)
        .setCustomBigContentView(remoteViews)
        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))
        ;

这篇关于FCM自定义通知声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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