跳到内容

Cordova 插件 API 参考

示例
var pushwoosh = cordova.require("pushwoosh-cordova-plugin.PushNotification");
// 应在 pushwoosh.onDeviceReady 之前调用
document.addEventListener('push-notification', function(event) {
var notification = event.notification;
// 在此处处理推送打开
});
// 初始化 Pushwoosh。这将在启动时触发所有待处理的推送通知。
pushwoosh.onDeviceReady({
appid: "XXXXX-XXXXX",
projectid: "XXXXXXXXXXXXXXX",
serviceName: "XXXX"
});
pushwoosh.registerDevice(
function(status) {
var pushToken = status.pushToken;
// 在此处处理成功注册
},
function(status) {
// 在此处处理注册错误
}
);

onDeviceReady

Anchor link to
PushNotification.prototype.onDeviceReady = function( config )

[android, ios, wp8, windows] 初始化 Pushwoosh 插件并触发启动推送消息。应在每次应用启动时调用。

config.appid – Pushwoosh 应用代码。

config.projectid – 适用于 Android 平台的 GCM 项目编号。

config.serviceName – 适用于 wp8 平台的 MPNS 服务名称。

示例
// 使用 projectid: "GOOGLE_PROJECT_NUMBER"、appid : "PUSHWOOSH_APP_ID"、serviceName : "WINDOWS_PHONE_SERVICE" 初始化 Pushwoosh。这将在启动时触发所有待处理的推送通知。
pushwoosh.onDeviceReady({
appid : "XXXXX-XXXXX",
projectid: "XXXXXXXXXXXXXXX",
serviceName: "XXXX"
});

registerDevice

Anchor link to
PushNotification.prototype.registerDevice = function( success, fail )

[android, ios, wp8, windows] 注册设备以接收推送通知并获取推送令牌 (Push Token)。

success – 成功回调。推送令牌作为 “status.pushToken” 参数传递给此回调。

fail – 错误回调。

示例
pushwoosh.registerDevice(
function(status) {
alert("已注册,推送令牌为: " + status.pushToken);
},
function(error) {
alert("注册失败: " + error);
}
);

unregisterDevice

Anchor link to
PushNotification.prototype.unregisterDevice = function( success, fail )

[android, ios, wp8, windows] 取消注册设备,使其不再接收推送通知。

success – 成功回调。

fail – 错误回调。

PushNotification.prototype.setTags = function( config, success, fail )

[android, ios, wp8, windows] 为设备设置标签 (Tag)。

参数

config – 包含自定义设备标签的对象。

success – 成功回调。推送令牌作为 “status.pushToken” 参数传递给此回调。

fail – 错误回调。

示例
// 设置标签:“deviceName” 值为 “hello”,“deviceId” 值为 10
pushwoosh.setTags({deviceName:"hello", deviceId:10},
function() {
console.warn('setTags 成功');
},
function(error) {
console.warn('setTags 失败');
}
);
// 设置列表标签 "MyTag",值为 (数组) "hello", "world"
pushwoosh.setTags({"MyTag":["hello", "world"]});
PushNotification.prototype.getTags = function( success, fail )

[android, ios, wp8, windows] 返回设备的标签,包括默认标签。

success – 成功回调。接收标签作为参数。

fail – 错误回调。

pushwoosh.getTags(
function(tags) {
console.warn('设备的标签: ' + JSON.stringify(tags));
},
function(error) {
console.warn('获取标签错误: ' + JSON.stringify(error));
}
);

getPushToken

Anchor link to
PushNotification.prototype.getPushToken = function( success )

[android, ios, wp8, windows] 如果可用,则返回推送令牌。请注意,该令牌也会在 registerDevice 函数回调中返回。

success – 成功回调。

pushwoosh.getPushToken(
function(token) {
console.warn('推送令牌: ' + token);
}
);

getPushwooshHWID

Anchor link to
PushNotification.prototype.getPushwooshHWID = function( success )

[android, ios, wp8, windows] 返回用于与 Pushwoosh API 通信的 Pushwoosh HWID。

successgetPushwooshHWID 回调。

pushwoosh.getPushwooshHWID(
function(token) {
console.warn('Pushwoosh HWID: ' + token);
}
);

getRemoteNotificationStatus

Anchor link to
PushNotification.prototype.getRemoteNotificationStatus = function( callback, error )

[android, ios] 返回推送通知权限的详细状态。

callback – 成功回调。接收具有以下属性的对象:

{
"enabled" : 通知启用标志。
"pushBadge" : 已授予角标权限。(仅限 iOS)
"pushAlert" : 已授予提醒权限。(仅限 iOS)
"pushSound" : 已授予声音权限。(仅限 iOS)
}

error — 错误回调。

setApplicationIconBadgeNumber

Anchor link to
PushNotification.prototype.setApplicationIconBadgeNumber = function( badgeNumber )

[android, ios] 设置应用图标角标数字。

badgeNumber – 图标角标数字。

getApplicationIconBadgeNumber

Anchor link to
PushNotification.prototype.getApplicationIconBadgeNumber = function( callback )

[android, ios] 返回应用图标角标数字。

callback – 成功回调。

pushwoosh.getApplicationIconBadgeNumber(function(badge){ alert(badge);} );

addToApplicationIconBadgeNumber

Anchor link to
PushNotification.prototype.addToApplicationIconBadgeNumber = function( badgeNumber )

[android, ios] 向应用图标角标添加一个值。

badgeNumber — 递增的图标角标数字。

getLaunchNotification

Anchor link to
PushNotification.prototype.getLaunchNotification = function( callback )

[android, ios] 如果应用是为响应推送通知而启动的,则返回推送通知的有效负载 (payload),否则返回 null。

callback – 成功回调。

clearLaunchNotification

Anchor link to
PushNotification.prototype.clearLaunchNotification = function( callback )

[android, ios] 清除启动通知,调用此方法后 getLaunchNotification() 将返回 null。

PushNotification.prototype.setUserId = function( userId )

[android, ios] 设置用户标识符 (User ID)——可以是 Facebook ID、用户名、电子邮件或任何其他用户 ID。这允许在多个用户设备之间匹配数据和事件。

userId – 用户字符串标识符。

PushNotification.prototype.postEvent = function( event, attributes )

[android, ios] 为应用内消息 (In-App Messages) 发布事件。这可以触发在 Pushwoosh 控制面板中指定的应用内消息显示。

event – 要触发的事件。

attributes – 包含额外事件属性的对象。

pushwoosh.setUserId("XXXXXX");
pushwoosh.postEvent("buttonPressed", { "buttonNumber" : 4, "buttonLabel" : "banner" });

createLocalNotification

Anchor link to
PushNotification.prototype.createLocalNotification = function( config, success, fail )

[android, ios] 安排本地通知。

config.msg – 通知消息。

config.seconds – 通知延迟(秒)。

config.userData – 在通知中传递的额外数据。

success – 成功回调。

fail – 错误回调。

pushwoosh.createLocalNotification({msg:"您的南瓜已准备就绪!", seconds:30, userData:{}})

clearLocalNotification

Anchor link to
PushNotification.prototype.clearLocalNotification = function()

[android] 清除由 createLocalNotification 创建的所有待处理的本地通知。

clearNotificationCenter

Anchor link to
PushNotification.prototype.clearNotificationCenter = function()

[android] 清除 Android 通知中心显示的所有通知。

setMultiNotificationMode

Anchor link to
PushNotification.prototype.setMultiNotificationMode = function( success, fail )

[android] 允许在 Android 通知中心显示多个通知。

setSingleNotificationMode

Anchor link to
PushNotification.prototype.setSingleNotificationMode = function( success,
fail )

[android] 只允许在 Android 通知中心显示最新的通知。

setSoundType

Anchor link to
PushNotification.prototype.setSoundType = function( type, success, fail )

[android] 为收到的推送设置默认声音。

type – 声音类型(0 – 默认,1 – 无声,2 – 始终)。

setVibrateType

Anchor link to
PushNotification.prototype.setVibrateType = function(type, success, fail )

[android] 为收到的推送设置默认振动模式。

type – 振动类型(0 – 默认,1 – 无振动,2 – 始终)。

setLightScreenOnNotification

Anchor link to
PushNotification.prototype.setLightScreenOnNotification = function( on, success, fail )

[android] 当通知到达时点亮屏幕。

on – 启用/禁用屏幕解锁(默认禁用)。

setEnableLED

Anchor link to
PushNotification.prototype.setEnableLED = function( on, success, fail )

[android] 当通知到达且屏幕关闭时,启用 LED 闪烁。

on – 启用/禁用 LED 闪烁(默认禁用)。

setColorLED

Anchor link to
PushNotification.prototype.setColorLED = function( color, success, fail )

[android] 设置 LED 颜色。与 setEnableLED 一起使用。

color – ARGB 整数格式的 LED 颜色。

getPushHistory

Anchor link to
PushNotification.prototype.getPushHistory = function( success )

[android] 返回已接收推送通知的数组。

success – 成功回调。

pushwoosh.getPushHistory(function(pushHistory) {
if(pushHistory.length == 0)
alert("无推送历史记录");
else
alert(JSON.stringify(pushHistory));
});
pushwoosh.clearPushHistory();

clearPushHistory

Anchor link to
PushNotification.prototype.clearPushHistory = function()

[android] 清除推送历史记录。

cancelAllLocalNotifications

Anchor link to
PushNotification.prototype.cancelAllLocalNotifications = function( callback )

[ios] 从通知中心清除所有本地通知。

presentInboxUI

Anchor link to

[android, ios] 打开收件箱屏幕。

PushNotification.prototype.presentInboxUI = function()

setCommunicationEnabled

Anchor link to

一个二进制方法,用于启用/禁用与 Pushwoosh 的所有通信。布尔值为 false 时,会取消设备接收推送通知的订阅,并停止下载应用内消息。值为 true 时,则反转该效果。

PushNotification.prototype.setCommunicationEnabled = function(enable, success, fail)

removeAllDeviceData

Anchor link to

删除有关设备的所有数据。

PushNotification.prototype.removeAllDeviceData = function()

push-receive

Anchor link to

[android, ios] 推送通知接收事件。当应用在前台或后台接收到推送通知时触发。已关闭的应用不会接收此事件。

事件属性

message – (string) 推送通知消息

userdata – (object/array) 推送通知自定义数据

onStart – (boolean) 是否为启动通知

foreground – (boolean) 是否在前台接收到通知

android – (object) Android 特定通知负载

ios – (object) iOS 特定通知负载

windows – (object) Windows 特定通知负载

document.addEventListener('push-receive',
function(event) {
var userData = event.notification.userdata;
if (typeof(userData) != "undefined") {
// 处理自定义通知数据
console.warn('用户数据: ' + JSON.stringify(userData));
}
}
);

前台通知

Anchor link to

默认情况下,Pushwoosh 插件不会在前台显示通知,而是自动触发 push-receive 事件。请参阅插件自定义指南来控制此行为。

push-notification

Anchor link to

[android, ios, wp8, windows] 推送通知接受事件。当用户点击推送通知时触发。

document.addEventListener('push-notification',
function(event) {
var message = event.notification.message;
var userData = event.notification.userdata;
if (typeof(userData) != "undefined") {
console.warn('用户数据: ' + JSON.stringify(userData));
}
}
);

事件属性

push-receive 相同

additionalAuthorizationOptions

Anchor link to

[仅限 ios] 提供[_额_外的通知授权选项。应在调用 registerDevice 之前调用。

pushwoosh.additionalAuthorizationOptions({
"UNAuthorizationOptionCriticalAlert" : 1,
"UNAuthorizationOptionProvisional": 0 // 如果您不想将其添加到您的应用中,请设置为 0 或不指定该选项。
});