app中可向微信申请一条公众号消息
官网链接:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/One-time_subscription_info.html
根据官网步骤一步步集成。
1.微信用户同意授权,获取一次给用户推送一条订阅消息的机会
WXSubscribeMsgReq \*req = [[WXSubscribeMsgReq alloc] init];
req.scene = scene;
req.templateId = templateId;
req.reserved = reserved;
[WXApi sendReq:req];
2.通过 API 推送订阅模板消息给到授权微信用户
POST https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=ACCESS_TOKEN
参数{"touser":"OPENID","template_id":"TEMPLATE_ID","url":"URL","scene":"SCENE","title":"TITLE","data":{"content":{"value":"VALUE","color":"COLOR"}}}
代码:
+(void)PostJsonHeader:(NSString *)header parameters:(NSDictionary *)parameters /*success:(successCallBack)success failure:(failureCallBack)failure*/{
AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:header parameters:parameters error:nil];
request.timeoutInterval=10.f;
[requestsetValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLSessionDataTask* task = [managerdataTaskWithRequest:requestcompletionHandler:^(NSURLResponse*_Nonnullresponse,id _NullableresponseObject,NSError*_Nullableerror) {
if(!error) {
if([responseObjectisKindOfClass:[NSDictionaryclass]]) {
//成功
}else{
}
}else{
//失败
}
}];
[taskresume];
}
附获取 access_token 说明:
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
注意点
1.消息为一次性消息,一次申请只能使用一次。
2.access_token需要获取,和网页授权token不同。
3.scene需要带入,否则用户拒绝接受。user refuse to accept the msg hint
4.格式不正确,检查参数,和请求方式。
网友评论