.Net 极光推送

401 阅读1分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

项目中需要用到消息推送,经部门决策后要用极光推送,于是服务端的我又要开始忙活了。

极光提供了C#的SDk,具体下载及文档docs.jiguang.cn/jpush/serve…

下载后根据文档一步步来。

首先新建自己的配置文件:

 public class JiguangConfig
    {
        public const string AppKey = "appkey";

        public const string Secret = "Secret";

        public const bool iOSOptions = true;
    }

注意这里的iOSOptions如果是测试环境需要设为false

然后就是推送设置:

 public class JPushHelper
    {

        private static JPushClient clientZG = new JPushClient(JiguangConfig.AppKey, JiguangConfig.Secret);

        public static string ExecutePushExample(List<string> usertokenList, string MessTitle, string MessContent, string MesType, string MesId, string RelationId, string Sound, string type)
        {
            var IosAlert = new
            {
                title = MessTitle,
                body = MessContent,
            };
            PushPayload pushPayload = new PushPayload()
            {
                Platform = new List<string> { "android", "ios" },
                Audience = new Audience()
                {
                    Alias = usertokenList,
                },
                Notification = new Notification
                {
                    Alert = MessContent,

                    Android = new Android
                    {
                        Alert = MessContent,
                        Title = MessTitle,
                        Sound = Sound,
                        Extras = new Dictionary<string, object>
                        {
                            ["MessageUrl"] = (Object)MesType,
                            ["Relationid"] = (Object)RelationId,
                            ["MessageId"] = (Object)MesId
                        }
                    },
                    IOS = new IOS
                    {
                        Alert = IosAlert,
                        Badge = "+1",
                        Sound = Sound + ".mp3",
                        Extras = new Dictionary<string, object>
                        {
                            ["MessageUrl"] = (Object)MesType,
                            ["Relationid"] = (Object)RelationId,
                            ["MessageId"] = (Object)MesId
                        }

                    }
                },
                Message = new Message
                {
                    Title = MessTitle,
                    Content = MessContent,
                    Extras = new Dictionary<string, string>
                    {
                        ["MessageUrl"] = MesType,
                        ["Relationid"] = RelationId,
                        ["MessageId"] = MesId
                    }
                },
                Options = new Options
                {
                    IsApnsProduction = JiguangConfig.iOSOptions // 设置 iOS 推送生产环境。不设置默认为开发环境。
                }
            };
            
            var response = clientZG.SendPush(pushPayload);
            return null;
        }
    }

到这一步其实推送就基本设置好了,But...

事件总不会那么顺利,总要设置一些磨难的。

移动端想实现一些自定义的声音或者推送的样式,这个系统自带的sdk就不支持了,为止,只能去下载开发包自己修改后编译,生成新的sdk; 下载的开发包内容

image.png 我们能在JPushClient中找到推送的方法

image.png

然后在Model中找到类Notification,在Android中添加Sound

image.png

这样我们在一开始的推送方法中就可以设置Sound了。

具体的我放到csdn了,有需要的可以下载看看download.csdn.net/download/we…