Android集成腾讯信鸽推送教程(二)---------项目代码开发设置

1、添加AndroidManifest.xml中声明的Receiver类

该类中定义了回调,通知等方法,在这里可以进行自定义消息处理逻辑,下面展示了两个方法,一个是当有状态栏通知消息推送到客户端时获取事件onNotifactionShowedResult 二是消息被点击后或者清楚后触发的事件onNotifactionClickedResult。

public class MessageReceiver extends XGPushBaseReceiver {
    public static final String LogTag = "TPushReceiver";
    private int NOTICE_ID=1000;

    // 通知展示
    @Override
    public void onNotifactionShowedResult(Context context,
                                          XGPushShowedResult notifiShowedRlt) {
        if (context == null || notifiShowedRlt == null) {
            return;
        }
        XGNotification notific = new XGNotification();
        notific.setMsg_id(notifiShowedRlt.getMsgId());
        notific.setTitle(notifiShowedRlt.getTitle());
        notific.setContent(notifiShowedRlt.getContent());
        // notificationActionType==1为Activity,2为url,3为intent
        notific.setNotificationActionType(notifiShowedRlt
                .getNotificationActionType());
        //Activity,url,intent都可以通过getActivity()获得
        notific.setActivity(notifiShowedRlt.getActivity());
        notific.setUpdate_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(Calendar.getInstance().getTime()));
        //NotificationService.getInstance(context).save(notific);
        //context.sendBroadcast(intent);
        Log.d("LCAAAAAAAAA", "+++++++++++++++++++++++++++++展示通知的回调" + notifiShowedRlt.toString());
    }

    // 通知点击回调 actionType=1为该消息被清除,actionType=0为该消息被点击。此处不能做点击消息跳转,详细方法请参照官网的Android常见问题文档
    @Override
    public void onNotifactionClickedResult(Context context,
                                           XGPushClickedResult message) {
        Log.e("LC", "+++++++++++++++ 通知被点击 跳转到指定页面。");
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        if (context == null || message == null) {
            return;
        }
        String text = "";
        if (message.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) {
            // 通知在通知栏被点击啦。。。。。
            // APP自己处理点击的相关动作
            // 这个动作可以在activity的onResume也能监听,请看第3点相关内容

//            Intent intent = new Intent(MainConstant.context, MessageDetailsActivity.class);
//            intent.putExtra("url", "http://www.baidu.com");
//            context.startActivity(intent);
            text = "通知被打开 :" + message;
        } else if (message.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) {
            // 通知被清除啦。。。。
            // APP自己处理通知被清除后的相关动作
            text = "通知被清除 :" + message;
        }
        Toast.makeText(context, "广播接收到通知被点击:" + message.toString(),
                Toast.LENGTH_SHORT).show();
        // 获取自定义key-value
        String customContent = message.getCustomContent();
        if (customContent != null && customContent.length() != 0) {
            try {
                JSONObject obj = new JSONObject(customContent);
                // key1为前台配置的key
                if (!obj.isNull("key")) {
                    String value = obj.getString("key");
                    Log.d(LogTag, "get custom value:" + value);
                }
                // ...
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        // APP自主处理的过程。。。
        Log.d(LogTag, text);
        show(context, text);
    }

    //反注册的回调方法

    //设置tag的回调方法

    //删除tag的回调

    //注册的回调方法

    // 消息透传的回调方法

    }

2、定义PushManage类用来集中配置推送信息

public class PushManager {

    private static PushManager pushManager;
    public static PushManager getInstence()
    {
        if (pushManager==null)
        {
            return new PushManager();
        }
        return pushManager;
    }
    public void SetConfig(String account)
    {
         //在进行测试环境的时候开启用来测试
        //XGPushConfig.enableDebug(MainConstant.context,true);

        //获取token
        //XGPushConfig.getToken(MainConstant.context);

        //信鸽推送注册方式分为token account tag, 这里因为app具有登录用户管理所以使用account登录账号方式注册
        //方便服务器在推送指定用户的时候通过account进行特定推送
        XGPushManager.registerPush(MainConstant.context,account, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object data, int flag) {
                //token在设备卸载重装的时候有可能会变
                Log.d("TPush", "注册成功,设备token为:" + data);
            }
            @Override
            public void onFail(Object data, int errCode, String msg) {
                Log.d("TPush", "注册失败,错误码:" + errCode + ",错误信息:" + msg);
            }
        });

//        // 新建自定义样式
//        XGBasicPushNotificationBuilder build = new XGBasicPushNotificationBuilder();
//        // 设置自定义样式属性,该属性对对应的编号生效,指定后不能修改。
//        build.setIcon(R.mipmap.ic_logo)
//                .setSound(
//                        RingtoneManager.getActualDefaultRingtoneUri(
//                                MainConstant.context,
//                                RingtoneManager.TYPE_ALARM)) // 设置声音
//                .setDefaults(Notification.DEFAULT_VIBRATE) // 振动
//                .setFlags(Notification.FLAG_NO_CLEAR); // 是否可清除
//        // 设置通知样式,样式编号为2,即build_id为2,可通过后台脚本指定
//        XGPushManager.setPushNotificationBuilder(MainConstant.context,
//                2, build);

    }

}

3、应用推送设置 在用户登陆成功后

//注册推送
PushManager.getInstence().SetConfig("15000000000");

到此,就可以在信鸽的后台进行推送调试了,如下图:
image
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,771评论 0 15
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,904评论 1 32
  • 推送技术产生场景: --服务器端主动性: 客户端与服务器交互都是客户端主动的, 服务器一般不能主动与客户端进行数据...
    原军锋阅读 35,089评论 4 60
  • 一、简历准备 1、个人技能 (1)自定义控件、UI设计、常用动画特效 自定义控件 ①为什么要自定义控件? Andr...
    lucas777阅读 10,648评论 2 54
  • 前言 在Android开发中,消息推送功能的使用非常常见。 推送消息截图 为了降低开发成本,使用第三方推送是现今较...
    BillyLu1994阅读 10,001评论 0 2

友情链接更多精彩内容