首先是服务端:
我们去极光官网下载相应的服务端SDK https://github.com/jpush/jpush-api-csharp-client/tree/v1 根据VS版本下载相应的SDK,我的是VS2013、Framwork4.0所有下载的是1.0版本的SDK
解压完
cn.jpush.api.dll和Newtonsoft.Json.dll添加到项目依赖中,注意:如果项目之前有依赖Newtonsoft.Json.dll先删除重新添加Newtonsoft.Json.dll的依赖
这个地根据自己的需求在你需要推送的地方加上下面的代码段就行,
PushPayload payload = PushObject_Android_Tag_AlertWithTitle(alert, title, equipmentCode);
try
{
//app_key 极光创建应用的APPK
//master_secret 极光创建应用的master_secret
JPushClient client = new JPushClient(app_key, master_secret);
var result = client.SendPush(payload);
由于统计数据并非即时的,所以等待一小段时间再执行下面的获取结果方法。
//System.Threading.Thread.Sleep(10000);
如需查询上次推送结果执行下面的代码。
//var apiResult = client.getReceivedApi(result.msg_id.ToString());
//var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
如需查询某个 messageId 的推送结果执行下面的代码。
//var queryResultWithV2 = client.getReceivedApi("1739302794");
//var querResultWithV3 = client.getReceivedApi_v3("1739302794");
}
catch (APIRequestException e)
{
Console.WriteLine("Error response from JPush server. Should review and fix it.");
Console.WriteLine("HTTP Status: " + e.Status);
Console.WriteLine("Error Code: " + e.ErrorCode);
Console.WriteLine("Error Message: " + e.ErrorMessage);
}
catch (APIConnectionException e)
{
Console.WriteLine(e.Message);
}
//构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知内容为 ALERT,标题为 TITLE。
public static PushPayload PushObject_Android_Tag_AlertWithTitle(string alert, string title, string shebei)
{
PushPayload pushPayload = new PushPayload();
pushPayload.platform = Platform.android();
pushPayload.audience = Audience.s_tag(shebei);
pushPayload.notification = Notification.android(alert, title);
return pushPayload;
}
服务端这边就已经完成了
接下来是Android端创建一个新的项目或者已有项目都行,然后需要在极光官网注册账号并创建一个应用,在推送设置里把你项目的包名填写在相应的地方保存即可,这样我们就能拿到我们的AppKey和Master Secret,替换到服务端的appk和master_secret;
Android端的集成分为两种方式:一种是手动集成,一种是自动集成,为了方便我这采用的是自动集成的方式。
在bulid.gradle的dependencies中添加依赖
compile 'cn.jiguang.sdk:jpush:3.1.6' // 此处以JPush 3.1.6 版本为例。
compile 'cn.jiguang.sdk:jcore:1.2.5' // 此处以JCore 1.2.5 版本为例。
在defaultConfig中加入以下配置
ndk {
//选择要添加的对应 cpu 类型的 .so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "您应用的APPKEY", //JPush 上注册的包名对应的 Appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
在application中进行初始化
JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
JPushInterface.init(this); // 初始化 JPush
到这一步就已经可以接收到推送通知了,注册一个处理接收到的自定义消息和点击通知的广播接收器
public class MyRecevicer extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){//接收到推送下来的自定义消息
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Log.i("wjp", "onReceive: 接收到推送下来的自定义消息"+message+extras);
}else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){//接收到推送下来的通知
Log.i("wjp", "onReceive: 接收到推送下来的通知");
}else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {//用户点击打开了通知
Log.i("wjp", "onReceive: 用户点击打开了通知");
}
}
}
在AndroidManifest中注册广播
android:name=".MyRecevicer"
android:exported="false"
android:enabled="true">
最后需要添加设置Tags
String deviceId = telephonyManager.getDeviceId();
Set tags = new LinkedHashSet();
tags.add(deviceId);
sequence++;
JPushInterface.addTags(getApplicationContext(), sequence, tags);
因为是需要根据自己的需求,配合后端服务向某单个设备或者某设备列表推送一条通知或者消息所以需要添加一个tag作为推送标识。