c#后台 極光推送到Android 和IOS客戶端


  由於項目需要,需要用到推送,然后就選擇了極光推送,下面記錄一下開發過程。

 

 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(); } } }

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM