信鸽推送用法

项目地址:http://download.csdn.net/detail/daidaishuiping/9752414
step1.注册并下载SDK  地址http://xg.qq.com/apps/ctr_app
应用创建成功后,点击“应用配置”即可看到APP专属的AccessId和AccessKey等信息。

step2.导入jar包


setp3.配置Androidmanifest.xml



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
        
            
                

                
            
        

        
        
            

                
                
            
        

        
        
            

                
                
                
                
                

                
                

                
                
                
                
            
            
            
                
                
                
                

                
            
        
        
        

        
        
            

                
                
            
        

        
        
        
        

    



step4.在activity中注册信鸽
public class MainActivity extends AppCompatActivity {

    Message m = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XGPushConfig.enableDebug(this, true);
        setContentView(R.layout.activity_main);
        getToken();


    }

    /**
     * 获取设备Token
     */
    private void getToken() {

        Handler handler = new HandlerExtension(MainActivity.this);
        m = handler.obtainMessage();
        // 注册接口
        XGPushManager.registerPush(getApplicationContext(), "单个账号",
                new XGIOperateCallback() {
                    @Override
                    public void onSuccess(Object data, int flag) {
                        Log.i("info", "+++ register push sucess. token:" + data);
                        m.obj = "+++ register push sucess. token:" + data;
                        m.sendToTarget();
                    }

                    @Override
                    public void onFail(Object data, int errCode, String msg) {
                        Log.i("info", "+++ register push fail. token:" + data
                                + ", errCode:" + errCode + ",msg:" + msg);

                        m.obj = "+++ register push fail. token:" + data
                                + ", errCode:" + errCode + ",msg:" + msg;
                        m.sendToTarget();
                    }
                });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        XGPushManager.onActivityStoped(this);
    }

    private static class HandlerExtension extends Handler {
        WeakReference mActivity;

        HandlerExtension(MainActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MainActivity theActivity = mActivity.get();
            if (theActivity == null) {
                theActivity = new MainActivity();
            }
            if (msg != null) {
                Log.i("info", "msg--->" + msg.obj.toString());
            }

        }
    }
}

setp5.如果要处理广播
创建一个广播类XGPushBaseReceiver
public class MessageReceiver extends XGPushBaseReceiver{

    @Override
    public void onRegisterResult(Context context, int i, XGPushRegisterResult xgPushRegisterResult) {

    }

    @Override
    public void onUnregisterResult(Context context, int i) {

    }

    @Override
    public void onSetTagResult(Context context, int i, String s) {

    }

    @Override
    public void onDeleteTagResult(Context context, int i, String s) {

    }


    /**
     * 消息透传
     * @param context
     * @param xgPushTextMessage
     */
    @Override
    public void onTextMessage(Context context, XGPushTextMessage xgPushTextMessage) {
        if(xgPushTextMessage!=null){
            Log.e("info","message:"+xgPushTextMessage.toString());
        }
    }

    /**
     * 通知点击回调
     * @param context
     * @param xgPushClickedResult
     */

    //可以直接在activity中获取,下面这个方法是写在activity中的
    //    protected void onResume() {
    //        // TODO Auto-generated method stub
    //        super.onResume();
    //        XGPushClickedResult click = XGPushManager.onActivityStarted(this);
    //        Log.d("TPush", "onResumeXGPushClickedResult:" + click);
    //        if (click != null) { // 判断是否来自信鸽的打开方式
    //            Toast.makeText(this, "通知被点击:" + click.toString(),
    //                    Toast.LENGTH_SHORT).show();
    //        }
    //    }
    @Override
    public void onNotifactionClickedResult(Context context, XGPushClickedResult xgPushClickedResult) {
            if(xgPushClickedResult!=null){
                Log.e("info","click:"+xgPushClickedResult.toString());
            }
    }

    /**
     * 通知的内容
     * @param context
     * @param xgPushShowedResult
     */
    @Override
    public void onNotifactionShowedResult(Context context, XGPushShowedResult xgPushShowedResult) {
        if(xgPushShowedResult!=null){
            Log.e("info","notifaction:"+xgPushShowedResult.toString());
        }
    }
}


注册广播
    
        
            
                
                
                
                
            
        

// 反注册,调用本接口后,APP将停止接收通知和消息
XGPushManager.unregisterPush(getApplicationContext());


你可能感兴趣的:(第三方框架的使用)