1. 程式人生 > >C#極光推送v3版本別名、notification、自定義訊息部分程式碼

C#極光推送v3版本別名、notification、自定義訊息部分程式碼

極光推送C#的伺服器端Demo對程式設計師不是很友好,所以把自己遇到的坑分享一下。PS.這篇文章得有看過部分相關知識的人才能理解,官網C#的demo我用不了,所以把我實現的方式貼了上來,不是解釋安裝教程用的是網上 引用dll方式下面是實現具體程式碼:引用的包名
using cn.jpush.api;
using cn.jpush.api.common;
using cn.jpush.api.push.mode;
using cn.jpush.api.common.resp;
using cn.jpush.api.push.notification;
private static string app_key_camp = ""; //自己申請        
private static string master_secret_camp = "";        private void SendPushCamp(String psd , LeaveInfo lea) //函式可以自己定義        
{            
    ArrayList list = new ArrayList();//因為採用別名的方式,所以建立了一個需要通知的使用者list列表            
    list.Add(psd);            
    string[] userIdArray = (string[])list.ToArray(typeof(string)); //轉換格式      //這是重點            
    JPushClient jPushclient = new JPushClient(app_key_camp, master_secret_camp);            
    PushPayload pushPayLoad = PushObject_Android_Alias_Alert(userIdArray,lea
    try { 
    var result = jPushclient.SendPush(pushPayLoad);
     }            
    catch (APIRequestException ex)            
    {                
    Console.WriteLine("Error response from JPush server. Should review and fix it. ");                
    Console.WriteLine("HTTP Status: " + ex.Status);                
    Console.WriteLine("Error Code: " + ex.ErrorCode);                
    Console.WriteLine("Error Message: " + ex.ErrorMessage);            }           
     catch (APIConnectionException ex) { Console.WriteLine(ex.Message); }        
    }                private PushPayload PushObject_Android_Alias_Alert(string[] userIdArray,LeaveInfo lea)        
{                       
        PushPayload pushPayload = new PushPayload()               {                
                   platform = Platform.android(),             
                   audience = Audience.s_alias(userIdArray),                
                   //notification = new Notification().setAlert(alertMsg), //單獨alert的時候使用                           
                 notification = new Notification().setAndroid(new AndroidNotification()
                                         .AddExtra("act", "leave") //自定義訊息這樣呼叫
                                        .AddExtra("billBH", lea.BillBH)                                                        
                                        .AddExtra("billDate", lea.BillDate)                                                        
                                        .AddExtra("userName", lea.UserName)                                                        
                                        .AddExtra("userDept", lea.UserDept
                                        .AddExtra("beginTime", lea.BeginTime)                                                       
                                        .AddExtra("endTime", lea.EndTime)                                                           
                                        .AddExtra("note", lea.Note)                                                                                                                          };            
    return pushPayload;        
}

自定義訊息是不能傳送通知的,所以需要選擇Notification來傳額外的資訊,我的LeaveInfo封裝了一個物件資訊,大家可以自己選擇自己需要的。還可以選擇setalert直接傳送通知

自定義訊息程式碼如下:

message = cn.jpush.api.push.mode.Message.content("自定義")  //內容   cn......mode中間可以省略,我是因為衝突才加的
                                        .AddExtras("carNm", "1")   //額外訊息,key,value形式
                                        .AddExtras("carId", "2")

第一次分享,寫的不好,各位見諒!