简体   繁体   中英

c# android GCM notification disable sound

I am trying to remove sound notification upon receiving gcm notification on android phone. I set data.sound=0 in my code but nothing. Android plays sound even app is closed! Is there any way to achieve my target? Thank you.

public void SendNotification(string deviceId, string message, int badgeCount) 
{
    string GoogleAppID = "....";
    var SENDER_ID = ".....";

    WebRequest webRequest;
    webRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
    webRequest.Method = "post";
    webRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
    webRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
    webRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

    string postData = string.Format("collapse_key=score_update&time_to_live=108&delay_while_idle=0&data.message={0} &data.time={1} &data.badge={3} &data.sound={4}&registration_id={2}", message, DateTime.UtcNow, regID, badgeCount, "default");

    Byte[] byteArray = Encoding.ASCII.GetBytes(postData);
    webRequest.ContentLength = byteArray.Length;
    Stream dataStream = webRequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    WebResponse webResponse = webRequest.GetResponse();
    dataStream = webResponse.GetResponseStream();
    using (StreamReader streamReader = new StreamReader(dataStream))
    {
        String sResponseFromServer = streamReader.ReadToEnd();
        streamReader.Close();
        dataStream.Close();
        webResponse.Close();
    }
}

You can use Notification.Builder with public method setDefaults .

As mentioned in setDefaults ,

Notification.Builder setDefaults (int defaults)

Set which notification properties will be inherited from system defaults.

Please note that value should be DEFAULT_SOUND to use the default notification sound.

Solutions given and a couple of comments in this SO post - How to change notification sound by code in android? hope helps you understand better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM