#ifndef _I_GT_PUSH_H_
#define _I_GT_PUSH_H_
/***************************版本修改日志***************************
* ==========V1.0.1, chenjb, 2014-07-28==========
* 1. 去除pushMessageToListB介面,pushMessageToListA改名為pushMessageToList
* 2. pushMessageToList增加回傳推送詳情功能
*
* ==========V1.0.2, chenjb, 2015-01-14==========
* 1. curl bug修改,http://blog.chinaunix.net/uid-20761674-id-3391182.html
*/
#ifdef __cplusplus
extern "C" {
#endif
#if defined(WIN32) || defined(_WIN32)
# define STDCALL _stdcall
# if defined(GTPUSHSDK_EXPORTS)
# define DLL_EXTERN __declspec(dllexport)
# else
# define DLL_EXTERN __declspec(dllimport)
# endif
#else
# define DLL_EXTERN
# define STDCALL
#endif
// 呼叫回傳結果結構體:SUCCESS(成功)/FAILED(失敗)
typedef enum result_t {
SUCCESS = 0,
FAILED = 1,
ERR_ENCODE = 2
} Result;
// 單個結果鍵值對結構體
typedef struct entry_t {
char key[100];
char value[1024];
} Entry;
// 推送結果結構體
typedef struct i_push_result_t {
int size; // 結果中存在多少個Entry
Entry entry[10];
} IPushResult;
// 基本訊息結構體
typedef struct message_t {
int isOffline;
long offlineExpireTime;
int priority;
} Message;
// 單個訊息結構體
typedef struct single_message_t {
Message msg;
} SingleMessage;
// CID串列訊息結構體
typedef struct list_message_t {
Message msg;
} ListMessage;
// 應用訊息結構體
typedef struct app_message_t {
Message msg;
char **appIdList;
int appIdListSize;
char **phoneTypeList;
int phoneTypeListSize;
char **provinceList;
int provinceListSize;
char **tagList;
int tagListSize;
} AppMessage;
// 推送目標結構體
typedef struct target_t {
char *appId;
char *clientId;
} Target;
// 模板型別列舉
typedef enum template_type_t {
Transmission, PopupTransmission, NotyPopLoad, Notification, Link
} TemplateType;
// 應用于IOS手機
typedef struct push_info_t {
char *actionLocKey;
int badge;
char *message;
char *sound;
char *payload;
char *locKey;
char *locArgs;
char *launchImage;
} PushInfo;
// 基本模板結構體
typedef struct template_t {
char *appId;
char *appKey;
PushInfo pushInfo;
} Template;
// 透傳模板結構體
typedef struct transmission_template_t {
Template t;
int transmissionType;
char *transmissionContent;
} TransmissionTemplate;
// 彈窗透傳模板結構體
typedef struct popup_transmission_template_t {
Template t;
int transmissionType;
char *transmissionContent;
char *title;
char *text;
char *img;
char *confirmButtonText;
char *cancelButtonText;
} PopupTransmissionTemplate;
// 通知彈窗下載模板結構體
typedef struct noty_pop_load_template_t {
Template t;
char *notyIcon;
char *logoUrl;
char *notyTitle;
char *notyContent;
int isCleared;
int isBelled;
int isVibrationed;
char *popTitle;
char *popContent;
char *popImage;
char *popButton1;
char *popButton2;
char *loadIcon;
char *loadTitle;
char *loadUrl;
int isAutoInstall;
int isActived;
char *androidMark;
char *symbianMark;
char *iphoneMark;
} NotyPopLoadTemplate;
// 通知模板結構體
typedef struct notification_template_t {
Template t;
int transmissionType;
char *transmissionContent;
char *text;
char *title;
char *logo;
char *logoUrl;
int isRing;
int isVibrate;
int isClearable;
} NotificationTemplate;
// 鏈接模板結構體
typedef struct link_template_t {
Template t;
char *text;
char *title;
char *logo;
char *logoUrl;
char *url;
int isRing;
int isVibrate;
int isClearable;
} LinkTemplate;
// 推送結果詳情
typedef struct push_detail_t {
char cid[33]; // 對應的CID
char ret[51]; // 詳情內容
} PushDetail;
// 功能:推送初始化,程式運行前初始化一次即可
// 引數:
// host 個推服務器URL [in]
// appKey 個推申請應用的appKey [in]
// masterSecret 個推申請應用的masterSecret [in]
// testUTF8 本庫所有介面必須傳入字符必須為UTF-8編碼,這里用于測驗是否是UTF-8編碼,固定填寫"編碼"兩字 [in]
// 回傳:Result列舉, SUCCESS、FAILED、ERR_ENCODE(編碼測驗失敗)
DLL_EXTERN Result STDCALL pushInit(char *host, char *appKey, char *masterSecret, const char *testUTF8);
// 功能:初始化個推服務器鑒權
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// 回傳:Result列舉, SUCCESS、FAILED
DLL_EXTERN Result STDCALL pushConnect(char *appKey);
// 功能:關閉個推服務器鑒權
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// 回傳:Result列舉, SUCCESS、FAILED
DLL_EXTERN Result STDCALL pushClose(char *appKey);
// 功能:推送單條訊息
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// msgData 單推訊息結構體指標 [in]
// templateData 模板結構體指標 [in]
// templateType 模板型別 [in]
// target 推送目標結構體指標 [in]
// 回傳:推送結果資料
DLL_EXTERN IPushResult STDCALL pushMessageToSingle(char *appKey, SingleMessage *msgData, void *templateData, TemplateType templateType, Target *target);
// 功能:獲取contentId,用于pushMessageToListA介面
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// msgData CID串列訊息結構體指標 [in]
// templateData 模板結構體指標 [in]
// templateType 模板型別 [in]
// contentId 用于回傳contentId的指標 [out]
// size 可存放contentId的大小 [in]
// 回傳:Result列舉, SUCCESS、FAILED
// 注意:如果size小于回傳的ID,則回傳FAILED
DLL_EXTERN Result STDCALL getContentId(char *appKey, ListMessage *msgData, void *templateData, TemplateType templateType, char *contentId, int size);
// 功能:取消contentId
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// contentId 需要取消的contentId [in]
// 回傳:Result列舉, SUCCESS、FAILED
DLL_EXTERN Result STDCALL cancelContentId(char *appKey, char *contentId);
// 功能:推送CID串列
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// contentId 由getContentId回傳的contentId [in]
// targetList 需要推送的目標串列 [in]
// targetSize 目標串列中有多少個Targe,建議每次50 [in]
// details 推送詳情內容,不需要詳情可填寫NULL,否則需要預先分配足夠記憶體(至少要targetSize個PushDetail) [in]
// 回傳:Result列舉, SUCCESS、FAILED
DLL_EXTERN IPushResult STDCALL pushMessageToList(char *appKey, char *contentId, Target *targetList, int targetSize, PushDetail *details);
// 功能:推送應用訊息
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// msgData 應用訊息結構體指標 [in]
// templateData 模板結構體指標 [in]
// templateType 模板型別 [in]
// 回傳:推送結果資料
DLL_EXTERN IPushResult STDCALL pushMessageToApp(char *appKey, AppMessage *msgData, void *templateData, TemplateType templateType);
// 功能:停止推送某個任務
// 引數:
// appKey 呼叫pushInit時APP對應的appKey [in]
// contentId 需要停止推送的contentId [in]
// 回傳:Result列舉, SUCCESS、FAILED
DLL_EXTERN Result STDCALL pushStop(char *appKey, char *contentId);
// 功能:獲取當前SDK版本號
// 引數:無
// 回傳:版本號字串
DLL_EXTERN const char * STDCALL getPushSdkVersion();
#ifdef __cplusplus
}
#endif
#endif
uj5u.com熱心網友回復:
#include "IGtPush.h"#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#endif
using namespace std;
static void printResult(IPushResult &result);
static char *getUTF8(const char *in);
static void releaseUTF8(const char *in);
// 注意:介面傳入字符必須為UTF-8編碼,因ASCII字符UTF-8編碼后與原先一樣,所以可以不編碼,但中文等非ASCII字符必須編碼
// 如果回傳的類似錯誤"post http data failed, code=6",錯誤碼可百度CURL回傳的錯誤碼是什么意思,如http://www.cnblogs.com/wainiwann/p/3492939.html
int main(int argc, char **argv) {
IPushResult result = {0};
// SDK版本號
printf("Current SDK version: %s\n", getPushSdkVersion());
char *encode = getUTF8("編碼");
// 初始化APP1
char *appId1 = "aK6jeksP5C7CsjSSEqLAA3";
char *appKey1 = "tpDVam96sY8pxhwBupJ4621";
char *masterSecret1 = "TBokfpttQJ6aHIhBE9y8671";
Result r1 = pushInit("http://sdk.open.api.igexin.com/apiex.htm", appKey1, masterSecret1, encode);
if(r1!=SUCCESS){
printf("pushInit for app1 failed: ret=%d\n", r1);
}
// 初始化APP2
char *appId2 = "jo9UBGGZIi63FrxuYGD9vA";
char *appKey2 = "pjHdwGdihP8kECA0ba1u371";
char *masterSecret2 = "8SY6Bjf8fU9KEYokAh2tX31";
Result r2 = pushInit("http://sdk.open.api.igexin.com/apiex.htm", appKey2, masterSecret2, encode);
if(r2!=SUCCESS){
printf("pushInit for app2 failed: ret=%d\n", r2);
}
releaseUTF8(encode);
// 構建訊息內容
Message msg = {0};
msg.isOffline = 1;
msg.offlineExpireTime = 3600000;
msg.priority = 1;
SingleMessage singleMsg = {0};
singleMsg.msg = msg;
// 模板內容
TransmissionTemplate templ = {0};
char *content1 = getUTF8("透傳訊息內容1");
templ.transmissionContent = content1;
templ.transmissionType = 1;
templ.t.appId = appId1;
templ.t.appKey = appKey1;
// 目標用戶
Target target1 = {0};
target1.appId = appId1;
target1.clientId = "2d5d1a37b50f843757fc85edb42ef17d";
// 推送訊息
cout << "============pushMessageToSingle to APP1============" << endl;
result = pushMessageToSingle(appKey1, &singleMsg, &templ, Transmission, &target1);
printResult(result);
// ##################以下結構同上面類似,用于推送訊息給APP2的用戶##################
Target target2 = {0};
target2.appId = appId2;
target2.clientId = "1624a0f884657f6c985b9beb5e4c432c";
TransmissionTemplate temp2 = {0};
char *content2 = getUTF8("透傳訊息內容2");
temp2.transmissionContent = content2;
temp2.transmissionType = 1;
temp2.t.appId = appId2;
temp2.t.appKey = appKey2;
cout << "============pushMessageToSingle to APP2============" << endl;
result = pushMessageToSingle(appKey2, &singleMsg, &temp2, Transmission, &target2);
printResult(result);
releaseUTF8(content2);
// ################################################################################
cout << "============pushMessageToList to APP1============" << endl;
ListMessage listMsg = {0};
listMsg.msg = msg;
char contentId[100] = {0};
ret = getContentId(appKey1, &listMsg, &templ, Transmission, contentId, sizeof(contentId));
if(ret == SUCCESS){ // 獲取ContentId成功
Target targets[2] = {0};
targets[0].appId = "aK6jeksP5C7CsjSSEqLAA3";
targets[0].clientId = "d9e6617c66a6a022af8ea6cda2078dda";
targets[1].appId = "aK6jeksP5C7CsjSSEqLAA3";
targets[1].clientId = "cb24dc9c0aaa0c3208e5216be423fed7";
PushDetail details[2] = {0}; // 推送詳情個數需要與目標個數相等
result = pushMessageToList(appKey1, contentId, targets, 2, details);
printResult(result);
cout << "*********************推送詳情*********************" << endl;
for(int i=0; i<2; i++){
cout << details[i].cid << ": " << details[i].ret << endl;
}
cout << "**************************************************" << endl;
// 取消contentId
ret = cancelContentId(appKey1, contentId);
cout << "cancelContentId ret: " << (ret==SUCCESS? "SUCCESS" : "FAILED") << endl;
} else {
cout << "獲取ContentId失敗" << endl;
}
releaseUTF8(content1);
return 0;
}
static void printResult(IPushResult &result) {
cout << "print result:-------------" << endl;
for (int i = 0; i < result.size; i++) {
cout << result.entry[i].key << ": " << result.entry[i].value << endl;
}
cout << "print end:----------------" << endl;
}
//GBK編碼轉換到UTF8編碼
static int GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen)
{
#ifdef WIN32
wchar_t * lpUnicodeStr = NULL;
int nRetLen = 0;
if(!lpGBKStr) //如果GBK字串為NULL則出錯退出
return 0;
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,NULL); //獲取轉換到Unicode編碼后所需要的字符空間長度
lpUnicodeStr = new WCHAR[nRetLen + 1]; //為Unicode字串空間
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen); //轉換到Unicode編碼
if(!nRetLen) //轉換失敗則出錯退出
return 0;
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,NULL); //獲取轉換到UTF8編碼后所需要的字符空間長度
if(!lpUTF8Str) //輸出緩沖區為空則回傳轉換后需要的空間大小
{
if(lpUnicodeStr)
delete []lpUnicodeStr;
return nRetLen;
}
if(nUTF8StrLen < nRetLen) //如果輸出緩沖區長度不夠則退出
{
if(lpUnicodeStr)
delete []lpUnicodeStr;
return 0;
}
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL); //轉換到UTF8編碼
if(lpUnicodeStr)
delete []lpUnicodeStr;
return nRetLen;
#else
return 0;
#endif
}
static char *getUTF8(const char *in){
#ifdef WIN32
int len = GBKToUTF8((unsigned char *)in, NULL, 0);
char *out = new char[len+1];
GBKToUTF8((unsigned char *)in, (unsigned char *)out, len);
return out;
#else
return (char *)in;
#endif
}
static void releaseUTF8(const char *in){
#ifdef WIN32
delete[] in;
#else
#endif
}
uj5u.com熱心網友回復:
這么長的東西,很少會有人細看的啊。一般來說 char* 用string 就行, char** 用pchar
uj5u.com熱心網友回復:
就是怎么在DELPHI中定義相對應的函式啊。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84805.html
上一篇:有一個奇怪的問題,尋高手解決
