[Turn] disposable micro-channel subscription message flow

Some time ago the project uses a micro-channel one-time subscription messages to guide the user to the landing micro letter public concern number. Disposable news subscription is authorized by the micro-channel users, third-party applications to get an opportunity to send a message notification to the micro-channel users; micro-channel users can not focus on the public number, every authorized time, the developer can lower the micro-letter first notification message, the message will be displayed in the micro-channel notification service.

Like the micro-channel message is divided into two steps:

First: micro-channel user authorization, once given the opportunity to send a message to the user.

 Random random = new Random();
 scene = random.nextInt(10000);
 if (WXUtil.isWeixinAvilible(FreeLearnActivity.this)){
       SubscribeMessage.Req req = new SubscribeMessage.Req();
       req.scene = scene;
       req.templateID = NeoConstantCode.WX_MODEL_ID;
       iwxapi.sendReq(req);
  }else{
       showToastMes("没有安装微信");
  }  


Parameters scene: Developers can fill in the number of plastic between 0-10000, used to identify the subscription field value;

       templateID: Subscribe message template id, review applications submitted after the passage of the open platform;

       When these two parameters are authorization requests must pass parameters, after authorization is successful, it will return the user's openid and other information, after the authorization is completed, the information required to return the callback method in onResp WXEntryActivity in:

@Override
public void onResp(BaseResp baseResp) {
    super.onResp(baseResp);
    if (baseResp.errCode == BaseResp.ErrCode.ERR_OK){
        if (ConstantsAPI.COMMAND_SUBSCRIBE_MESSAGE == baseResp.getType()){
            Intent intent = new Intent(NeoConstantCode.REFRESH_FREELEARN);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (android.os.Build.VERSION.SDK_INT >= 12) {
                intent.setFlags(32 );
            }
            intent.putExtra("openid",baseResp.openId);
            NeoApplication.getContext().sendBroadcast(intent);
        }
    }
    finish();
} 


For receiving information back to the beginning when it came to writing a small dip, after the authorization is completed, onResp () method does not receive any news, of course, people who want to approach the onCreat

iwxapi = WXAPIFactory.createWXAPI(FreeLearnActivity.this,NeoConstantCode.WX_APP_ID);
iwxapi.registerApp(NeoConstantCode.WX_APP_ID);
iwxapi.handleIntent(getIntent(),this); 


But to be noted here, an inattentive will fall into the pit, for WXEntryActivity micro-channel document says inherited from Activity, achieve IWXAPIEventHandler; then the callback information onResp () method, but general engineering will access such as Friends of the Union party tools like the Friends of the Union, they inherited from WXCallBackActivity (), has been packaged the micro-channel configuration document said, so after accessing the League of Friends, we only need to rewrite onResp () can be, we must not forget super.onResp ( ), so you can receive the callback information; of course a little, after micro-channel version 5.1.6 opensdk access required for the job, or there will be problems respType = null, and after version 5.1.6 fixes this bug; receiving a callback after the message can be sent to the page openid their need to operate, the next step;

Second: Send a message to the micro-channel users;

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

Interface obtain access_token get request by the above, of course, as the document says, access_token recommended saved on the server, front-end request is not recommended, because the field is the number of requests per day with an upper limit, each user will request a refresh, the upper limit is reached, it will affect use app; back through the interface to the distal end of the field issued; invoking the following interfaces later get access_token

http请求方式: post
https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=ACCESS_TOKEN

After the success of the micro-channel user can receive a service notification that the content of the parameter setting interface; At this point, a complete end to send the message flow;

Small note: After one-time messages can not be authorized to return as like to share your app, I used method in the project is to switch applications running in the background to the foreground, the code below, record it:

/ ** * Get the ActivityManager / 
the ActivityManager ActivityManager = (the ActivityManager) context.getSystemService (ACTIVITY_SERVICE); 
/ * get task (task) * / current running 
List <ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks (100); 
for ( taskInfo ActivityManager.RunningTaskInfo: taskInfoList) { 
    / ** find the task of the present application, and switch it to the front * / 
    IF (taskInfo.topActivity.getPackageName () the equals (context.getPackageName ())) {. 
        activityManager.moveTaskToFront (taskInfo .id, 0); 
        BREAK; 
    } 
}


Record what problems they have encountered, to sum up, dedicated to the advancement of their own a little every day, come on!
from: https: //blog.csdn.net/qiaoxiaoguang/article/details/84192491

Guess you like

Origin www.cnblogs.com/xuan52rock/p/11347198.html