FCM自定义通知声音

3

我在我的安卓应用中使用Firebase消息传递。我正在使用Firebase发送推送通知。我想将默认通知音更改为自定义音频。我该怎么做?

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

1
你可以找到这篇文章非常有用。它解释了如何处理自定义声音。文章链接:https://www.myflashlabs.com/custom-icon-sound-fcm-air-native-extension/ - Ergin Ersoy
2个回答

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

使用上述代码可以从资源中添加自定义声音。

如果我们使用Notification类,则可以使用上述代码。

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

如果您正在使用NotificationBuilder,请使用以下代码。

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

我应该在哪里替换它?我们能用我们自己的代码修改上面的代码吗? - Jack N
它[运行成功]非常感谢你,兄弟:)它帮了我很多:) - Jack N
很高兴能够帮忙。请接受这个答案,因为它是有效的。 :) - TheHound.developer

3
使用setSound()方法来设置声音。
if(!silent) {   // check if phone is not in silent mode
       notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
       NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

       notificationManager.notify(9999, notificationBuilder.build());
    }
}

或者您可以使用

{
    "to" : "XXYYXXYY...",

    "notification" : {
         "body" : "The stock opened on a bullish note at Rs. 449 and touched a high of Rs. 461.35, up 5.06 per cent over its previous closing price on the BSE. A similar movement was seen on the NSE where the stock opened at Rs. 450 and hit a high of Rs. 463.70, up 5.32 per cent.",
         "title" : "Stocks in focus: Kalpataru Power, Punj Lloyd, J B Chem, Bharti Airtel",
         "icon" : "ic_stock",
         "sound" : "res_notif_sound"
    }
 }

如果你想使用设备的默认声音,你应该使用:"sound": "default"。

@JackN,无法理解你的评论。 - Rinav
res_notif_sound是系统文件吗?如果我们想引用res文件夹中的文件,应该怎么做? - Anu Bhalla
谢谢,它帮了我很多。 - Anu Bhalla
@AnuChaudhary 很高兴能帮忙 ;) - Rinav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接