• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#后台极光推送到Android和IOS客户端

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  由于项目需要,需要用到推送,然后就选择了极光推送,下面记录一下开发过程。

 

 1. 首先去极光推送官网(https://www.jpush.cn/)申请一个账号。

 2. 进入控制台,创建一个新引用,输入正确的Android的包名。

 3. 此时你就获取到了一个AppKey 和一个 Master Secret。 此时你可以下载一下Android的Demo, 去控制台发个信息体验一下

  

 

 

  4, 去下载一个C# 的SDK 

  

  

 

5. 引用这个DLL

6. 

  这里有一点要注意!!

  复制appKey 和masterSecret是,要确保每空格, 特别是masterSecret 后面容易有个空格。这时候就会出现没授权 400错误

 

using System;

using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using ifunction.JPush.V3;
using Newtonsoft.Json;
namespace ifunction.JPush.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var appKey = "1234567890abcdef"; // Your App Key from JPush
            var masterSecret = "1234567890abcdef"; // Your Master Secret from JPush


            Dictionary<string, string> customizedValues = new Dictionary<string, string>();
            customizedValues.Add("CK1", "CV1");
            customizedValues.Add("CK2", "CV2");
  

            JPushClientV3 client = new JPushClientV3(appKey, masterSecret);
            Audience audience = new Audience();
            // In JPush V3, tag can be multiple added with different values.
            // In following code, it is to send push to those who are in ((Tag1 AND Tag2) AND (Tag3 OR Tag4))
            // If you want to send to all, please use: audience.Add(PushTypeV3.Broadcast, null);
       

        /// 请在下面1234中选择一个你想要的推送的方式,我用的是别名
        //1. 发送全部 
        audience.Add(PushTypeV3.Broadcast, null);
        //2. 设置tag发送
        audience.Add(PushTypeV3.ByTagWithinAnd, new List<string>(new string[] { "Tag1", "Tag2" }));
        audience.Add(PushTypeV3.ByTagWithinOr, new List<string>(new string[] { "Tag3", "Tag4" }));
        //3. 设置RegistrationId发送
        audience.Add(PushTypeV3.ByRegistrationId, new List<string>(new string[] { "1111111111111111" })); 
        //4. 设置别名发送
        audience.Add(PushTypeV3.ByAlias, new List<string>(new string[] { "Alias1", "Alias2" }));


 In JPush V3, Notification would not be display on screen, it would be transferred to app instead.
            // And different platform can provide different notification data.
            Notification notification = new Notification
            {
                AndroidNotification = new AndroidNotificationParameters
                {
                    Title = "JPush provides V3.",
                    Alert = "JPush V2 would be retired soon.",
                    CustomizedValues = customizedValues
                },
                iOSNotification = new iOSNotificationParameters
                {
                    Badge = 1,
                    Alert = "JPush V2 would be retired soon.",
                    Sound = "YourSound",
                    CustomizedValues = customizedValues
                }
            };
  

            var response = client.SendPushMessage(new PushMessageRequestV3
            {
                Audience = audience,
                Platform = PushPlatform.AndroidAndiOS,
                IsTestEnvironment = true,
                AppMessage = new AppMessage
                {
                    Content = "Hello, this is a test push of V3 from .NET. Have a nice day!",
                    CustomizedValue = customizedValues
                },
                Notification = notification
            });
  

            Console.WriteLine(response.ResponseCode.ToString() + ":" + response.ResponseMessage);
            Console.WriteLine("Push sent.");
            Console.WriteLine(response.ResponseCode.ToString() + ":" + response.ResponseMessage);
  

            List<string> idToCheck = new List<string>();
            idToCheck.Add(response.MessageId);
  

            var statusList = client.QueryPushMessageStatus(idToCheck);
  

            Console.WriteLine("Status track is completed.");
            if (statusList != null)
            {
                foreach (var one in statusList)
                {
                    Console.WriteLine(string.Format("Id: {0}, Android: {1}, iOS: {2}", one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount));
                }
            }
  

            Console.WriteLine("Press any key to exit.");
            Console.Read();
        }
    }
}

 

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
服务调试方法c#写windows服务发布时间:2022-07-13
下一篇:
C#中&quot;this&quot;引用发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap