簡體   English   中英

FCM自定義通知聲音

[英]FCM Custom notification sound

我的Andriod應用程序具有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());
}
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))
        ;

使用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”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM