.Net Jpush极光推送

1.首先登陆极光官网 注册账号   https://www.jiguang.cn/push
 
2.注册成功后,在应用管理中 创建应用
 
3.创建应用成功后,能查看引用的详细信息,这里需要记住应用的 AppKey 和 Master Secret
 
4.配置完成后
根据官方给的dll和实例代码,进行配置。
提取码:zas3
 
5.根据实例代码进行改造
以下是官方给的示例代码:
 1 private static JPushClient client = new JPushClient("AppKey的值", "Master Secret的值");
 2         public static void Main(string[] args)
 3         {
 4             ExecutePushExample();
 5             ExecuteDeviceEample();
 6             ExecuteReportExample();
 7             ExecuteScheduleExample();
 8             Console.ReadLine();
 9         }
10         private static void ExecutePushExample()
11         {
12             PushPayload pushPayload = new PushPayload()
13             {
14                 Platform = new List<string> { "android", "ios" },
15                 Audience = "all",
16                 Notification = new Notification
17                 {
18                     Alert = "hello jpush",
19                     Android = new Android
20                     {
21                         Alert = "android alert",
22                         Title = "title"
23                     },
24                     IOS = new IOS
25                     {
26                         Alert = "ios alert",
27                         Badge = "+1"
28                     }
29                 },
30                 Message = new Message
31                 {
32                     Title = "message title",
33                     Content = "message content",
34                     Extras = new Dictionary<string, string>
35                     {
36                         ["key1"] = "value1"
37                     }
38                 },
39                 Options = new Options
40                 {
41                     IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
42                 }
43             };
44             var response = client.SendPush(pushPayload);
45             Console.WriteLine(response.Content);
46         }
以下是改造的代码:
注: 如果要发送给指定设备,需要传递参数:设备ID
 1 //配置信息
 2    public static String TITLE = "Test from C# v3 sdk";
 3     public static String ALERT = "Test from  C# v3 sdk - alert";
 4     public static String MSG_CONTENT = "Test from C# v3 sdk - msgContent";
 5     public static String REGISTRATION_ID = "**********";  //硬件设备ID
 6     public static String SMSMESSAGE = "Test from C# v3 sdk - SMSMESSAGE";
 7     public static int DELAY_TIME = 1;
 8     public static String TAG = "tag_api";
 9     public static String app_key =  System.Configuration.ConfigurationManager.AppSettings["app_key"];
10     public static String master_secret =  System.Configuration.ConfigurationManager.AppSettings["master_secret"];
11 
12 //初始化   
13 JPushClient client = new JPushClient(app_key, master_secret);
14 PushPayload pushmsg = PushMessageWithExtras(type, outcode);
15 //极光发送
16 client.SendPush(pushmsg);
17 
18 //PushMessageWithExtras方法
19   public static PushPayload PushMessageWithExtras(int type, string outcode)
20     {
21         var pushPayload = new PushPayload();
22         pushPayload.platform = Platform.all();
23         pushPayload.audience = Audience.s_registrationId(REGISTRATION_ID);
24         var notification = new Notification();
25         //推送中 传递额外的参数 “type”和“"outcode"”
26         notification.AndroidNotification = new  AndroidNotification().setAlert(ALERT).AddExtra("type", type).AddExtra("outcode",  outcode);
27         pushPayload.notification = notification;
28         pushPayload.message = Message.content(MSG_CONTENT);
29         return pushPayload;
30     }
 

猜你喜欢

转载自www.cnblogs.com/ywkcode/p/11135363.html