简体   繁体   English

C#Android GCM通知禁用声音

[英]c# android GCM notification disable sound

I am trying to remove sound notification upon receiving gcm notification on android phone. 我试图在Android手机上收到gcm通知后删除声音通知。 I set data.sound=0 in my code but nothing. 我在代码中设置了data.sound=0 ,但是什么也没有。 Android plays sound even app is closed! 即使应用已关闭,Android也会播放声音! 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 . 您可以将Notification.Builder与公共方法setDefaults

As mentioned in setDefaults , 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. 请注意,该值应为DEFAULT_SOUND才能使用默认的通知声音。

Solutions given and a couple of comments in this SO post - How to change notification sound by code in android? 在此SO帖子中给出了解决方案和一些评论- 如何通过android中的代码更改通知声音? hope helps you understand better. 希望可以帮助您更好地了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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