订阅步骤
- 主动拉起订阅,使用接口wx.requestSubscribeSystemMessage(Object object)
- 如果用户拒绝订阅,下次点击订阅后打开设置页,在设置页回来后通过接口wx.getSetting(Object object)获取用户是否在设置页已经打开了订阅开关
typescript
public Subscribe(cb?: Function) {
if (!this._sdk) { return; }
if (this._Subscribed === 'accept') {
Log.log("wx Subscribe 已订阅:", this._Subscribed);
cb && cb(1);
return;
}
let request = () => {
this._sdk!.requestSubscribeSystemMessage({
msgTypeList: ['SYS_MSG_TYPE_WHATS_NEW'],
success: (res:any) => {
this._Subscribed = res['SYS_MSG_TYPE_WHATS_NEW'];
if (this._Subscribed === 'accept') {
Log.log("wx Subscribe 订阅成功:", this._Subscribed);
cb && cb(0);
} else {
Log.log("wx Subscribe 订阅失败:", this._Subscribed);
cb && cb(-1);
}
},
fail: (res) => {
Log.log("wx Subscribe 订阅失败:", res);
cb && cb(-1);
}
})
}
let openSetting = () => {
this._sdk!.openSetting({
withSubscriptions: true,
success:()=>{
this._waitSubscribe = true;
},
fail: (res) => {
cb && cb(-1);
},
});
}
if (!this._Subscribed || this._Subscribed.length == 0) {
request();
} else {
openSetting();
}
}
public onShow(callback?: Function) {
if (!this._sdk) {
return;
}
this._sdk!.onShow((res: WechatMinigame.OnShowListenerResult) => {
if (this._waitSubscribe) {
this._waitSubscribe = false;
this.checkSubscribe(true);
}
});
}
private checkSubscribe(bNeedEvent : boolean) {
if (!this._sdk) {
return;
}
if (this._Subscribed === 'accept') {
return;
}
this._sdk.getSetting({
withSubscriptions: true,
success: (res) => {
if (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings.SYS_MSG_TYPE_WHATS_NEW) {
this._Subscribed = res.subscriptionsSetting.itemSettings.SYS_MSG_TYPE_WHATS_NEW;
Log.log("wx Subscribe 订阅状态:", this._Subscribed);
if (bNeedEvent && this._Subscribed === 'accept') {
//TO DO SOMTHING
}
}
},
fail: (res) => {
Log.log("wx getSetting fail:", res);
}
});
}