概述
这是 JPush REST API 的 C# 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能。
对应的 REST API 文档:http://docs.jpush.io/server/rest_api_v3_push/
环境配置
在 jpush-api-csharp-client 项目根目录可以下载下面的两个文件。
-
在项目引用中添加依赖包: Newtonsoft.Json.dll
-
在项目引用中添加: cn.jpush.api.dll
Push API v3
向某单个设备或者某设备列表推送一条通知或者消息
推送的载体:PushPayload
对应 REST API 中 Push API v3 的 json 格式说明文档 http://docs.jpush.io/server/rest_api_v3_push/
-
推送的载体:类PushPayload
-
Platform 平台信息(必填)
-
Audience 推送目标(必填)
-
Notification 通知内容(可选)
-
Message 消息内容(可选)
-
Options 可选参数(可选)
推送样例
进行推送的关键在于构建一个 PushPayload 对象。以下示例一般的构建对象的用法。
- 快捷地构建推送对象:所有平台,所有设备,内容为 ALERT 的通知。
public static PushPayload PushObject_All_All_Alert() { PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.all(); pushPayload.audience = Audience.all(); pushPayload.notification = new Notification().setAlert(ALERT); return pushPayload; }
- 构建推送对象:所有平台,推送目标是别名为 "alias1",通知内容为 ALERT。
public static PushPayload PushObject_all_alias_alert() { PushPayload pushPayload_alias = new PushPayload(); pushPayload_alias.platform = Platform.android(); pushPayload_alias.audience = Audience.s_alias("alias1"); pushPayload_alias.notification = new Notification().setAlert(ALERT); return pushPayload_alias; }
- 构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE。
public static PushPayload PushObject_Android_Tag_AlertWithTitle() { PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.android(); pushPayload.audience = Audience.s_tag("tag1"); pushPayload.notification = Notification.android(ALERT,TITLE); return pushPayload; }
- 构建推送对象:平台是 iOS,推送目标是 "tag1", "tag_all" 的并集,推送内容同时包括通知与消息 - 通知信息是 ALERT,角标数字为 5,通知声音为 "happy",并且附加字段 from = "JPush";消息内容是 MSG_CONTENT。通知是 APNs 推送通道的,消息是 JPush 应用内消息通道的。APNs 的推送环境是“生产”(如果不显式设置的话,Library 会默认指定为开发)
public static PushPayload PushObject_ios_tagAnd_alertWithExtrasAndMessage() { PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.android_ios(); pushPayload.audience = Audience.s_tag_and("tag1", "tag_all"); var notification = new Notification(); notification.IosNotification = new IosNotification().setAlert(ALERT) .setBadge(5) .setSound("happy") .AddExtra("from","JPush"; pushPayload.notification = notification; pushPayload.message = Message.content(MSG_CONTENT); return pushPayload; }
-
C# SDK 下载地址:https://github.com/jpush/jpush-api-csharp-client