Android 推送-个推

来源:互联网 发布:淘宝网店一件代发货源 编辑:程序博客网 时间:2024/04/30 19:50

最近闲来无事就把个推研究了一下,发现个推的SDK版本已经更新到2.9.5.0了,而且跟以前的版本相比感觉变化实在是太大了。
以前的版本在配置清单里面的配置为

<!-- 配置第三方Receiver-->        <receiver            android:name="cn.com.zhaoshuikan.bdhospital.PushReceiver"            android:exported="false" >            <intent-filter>                <action android:name="com.igexin.sdk.action.bDEt739zAr9OJJfcUu0hL7" />            </intent-filter>        </receiver>         <!-- 配置SDK核心服务  -->        <service            android:name="com.igexin.sdk.PushService"            android:exported="true"            android:label="NotificationCenter"            android:process=":pushservice" >        </service>        <receiver android:name="com.igexin.sdk.PushReceiver" >            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />                <action android:name="android.intent.action.USER_PRESENT" />                <action android:name="com.igexin.sdk.action.refreshls" />                   <!-- 以下四项为可选的action声明,可大大提高service存活率和消息到达速度  -->                <action android:name="android.intent.action.MEDIA_MOUNTED" />                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />            </intent-filter>        </receiver>        <receiver            android:name="com.igexin.sdk.PushManagerReceiver"            android:exported="false" >            <intent-filter>                <action android:name="com.igexin.sdk.action.pushmanager" />            </intent-filter>        </receiver>        <provider android:name="com.igexin.download.DownloadProvider"            android:exported="true"            android:authorities="downloads.cn.com.zhaoshuikan.bdhospital"            android:process=":pushservice" />        <service            android:name="com.igexin.download.DownloadService"            android:process=":pushservice" />        <receiver android:name="com.igexin.download.DownloadReceiver" >            <intent-filter>                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />            </intent-filter>        </receiver>        <receiver            android:name="com.igexin.getuiext.service.PayloadReceiver"            android:exported="false" >            <intent-filter>                <!-- 这个com.igexin.sdk.action.7fjUl2Z3LH6xYy7NQK4ni4固定,不能修改-->                <action android:name="com.igexin.sdk.action.7fjUl2Z3LH6xYy7NQK4ni4" />                 <!-- android:name="com.igexin.sdk.action.第三方的appId" -->                <action android:name="com.igexin.sdk.action.bDEt739zAr9OJJfcUu0hL7" />            </intent-filter>        </receiver>        <service            android:name="com.igexin.getuiext.service.GetuiExtService"            android:process=":pushservice" />        <service android:name="cn.com.zhaoshuikan.bdhospital.utils.downloadupdate.UpdateDownloadService">        </service>

而且以前的接收推送的是继承自BroadcastReceiver的

public class PushReceiver extends BroadcastReceiver {    /** Notification构造器 */    NotificationCompat.Builder mBuilder;    private Context context;    private NotificationManager mNotificationManager;    private Notification mNotification = null; // 通知    public static final int NOTIFICATION_ID = 200; // 通知唯一id    /**     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息     */    private Map<String, String> payloadData = new HashMap<String, String>();    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        switch (bundle.getInt(PushConsts.CMD_ACTION)) {        case PushConsts.GET_MSG_DATA:            // 获取透传数据            // String appid = bundle.getString("appid");            byte[] payload = bundle.getByteArray("payload");            String taskid = bundle.getString("taskid");            String messageid = bundle.getString("messageid");            // smartPush第三方回执调用接口,actionid范围为90000-90999,可根据业务场景执行            boolean result = PushManager.getInstance().sendFeedbackMessage(                    context, taskid, messageid, 90001);            System.out.println("第三方回执接口调用" + (result ? "成功" : "失败"));            if (payload != null) {                String data = new String(payload);                }            break;        case PushConsts.GET_CLIENTID:            // 获取ClientID(CID)            // 第三方应用需要将CID上传到第三方服务器,并且将当前用户帐号和CID进行关联,以便日后通过用户帐号查找CID进行消息推送            String cid = bundle.getString("clientid");            System.out.println(">>>>>>>>>cid:" + cid);                break;        case PushConsts.THIRDPART_FEEDBACK:                        break;        default:            break;        }    }

相信用过个推的对这些代码并不陌生,但是今天当我再去看的时候,发现发生的变化实在是太大了。之前的推送的消息提醒是需要自己去写的,而且当有推送的时候信息payload的值是很快就能获取到的,而且就是我们在官网测试平台输入的一些标题和内容,但是现在的变化是如果你只设置标题和内容不去设置透传的话,你获取到的payload永远只会是null。这些信息应该如何去配置呢?
这里写图片描述
之前我们只输入标题和内容应该是可以获取到payload的值的,但是现在我们还要去点击高级设置
这里写图片描述
这里写图片描述
这样设置以后我们才能获取到payload的值,而且payload的值就是我们设置的透传信息的内容,是一个json串。
之前的

PushManager.getInstance().initialize(this.getApplicationContext());

这行代码现在新版本多了一个参数,而且还需要注册服务现在已经变成了

 PushManager.getInstance().initialize(this.getApplicationContext(),DemoPushService.class); PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), PushReceiver.class);
package com.lyxrobert.pushinfo;import android.content.Context;import android.os.Message;import android.util.Log;import com.igexin.sdk.GTIntentService;import com.igexin.sdk.PushManager;import com.igexin.sdk.message.GTCmdMessage;import com.igexin.sdk.message.GTTransmitMessage;public class PushReceiver extends GTIntentService {    @Override    public void onReceiveServicePid(Context context, int pid) {    }    @Override    public void onReceiveClientId(Context context, String clientid) {        sendMessage(clientid, 1);    }    @Override    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {        String appid = msg.getAppid();        String taskid = msg.getTaskId();        String messageid = msg.getMessageId();        byte[] payload = msg.getPayload();        String pkg = msg.getPkgName();        String cid = msg.getClientId();        boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);        Log.d(TAG, "call sendFeedbackMessage = " + (result ? "success" : "failed"));        if (payload == null) {        } else {            String data = new String(payload);            sendMessage(data, 0);            System.out.println("data-------->"+data);        }    }    @Override    public void onReceiveOnlineState(Context context, boolean online) {    }    @Override    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {    }    private void sendMessage(String data, int what) {        Message msg = Message.obtain();        msg.what = what;        msg.obj = data;        PushApplication.sendMessage(msg);    }}

配置清单里面的信息

 <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.lyxrobert.pushinfo">    <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>    <uses-permission android:name="android.permission.WAKE_LOCK"/>    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.VIBRATE"/>    <uses-permission android:name="android.permission.GET_TASKS"/>    <!-- 支持iBeancon 需要蓝牙权限 -->    <uses-permission android:name="android.permission.BLUETOOTH"/>    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>    <!-- 支持个推3.0 电子围栏功能 -->    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>    <!-- 浮动通知权限 -->    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>    <uses-permission android:name="getui.permission.GetuiService.com.lyxrobert.pushinfo"/>    <permission        android:name="getui.permission.GetuiService.com.lyxrobert.pushinfo"        android:protectionLevel="normal"/>    <application        android:name="com.lyxrobert.pushinfo.PushApplication"        android:allowBackup="true"        android:icon="@drawable/photo"        android:theme="@style/AppTheme"        android:label="@string/app_name" >        <!-- 第三方应用配置 -->        <meta-data            android:name="PUSH_APPID"            android:value="JChyfqDCbvAJf5NMLEXr1A"/>        <meta-data            android:name="PUSH_APPKEY"            android:value="h0uZ4AMTLj7WHg0z6GiNt7"/>        <meta-data            android:name="PUSH_APPSECRET"            android:value="Bz5sUvrG546b1B7uEMbVo2"/>        <activity android:name="com.lyxrobert.pushinfo.MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".PushReceiver"/>        <!-- 配置SDK核心服务 -->        <service            android:name="com.igexin.sdk.PushService"            android:exported="true"            android:label="NotificationCenter"            android:process=":pushservice">            <intent-filter>                <action android:name="com.igexin.sdk.action.service.message"/>            </intent-filter>        </service>        <receiver            android:name="com.igexin.sdk.PushReceiver">            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED"/>                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>                <action android:name="android.intent.action.USER_PRESENT"/>                <action android:name="com.igexin.sdk.action.refreshls"/>                <action android:name="android.intent.action.MEDIA_MOUNTED"/>                <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>            </intent-filter>        </receiver>        <activity            android:name="com.igexin.sdk.PushActivity"            android:excludeFromRecents="true"            android:exported="false"            android:process=":pushservice"            android:taskAffinity="com.igexin.sdk.PushActivityTask"            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>        <activity            android:name="com.igexin.sdk.GActivity"            android:excludeFromRecents="true"            android:exported="true"            android:process=":pushservice"            android:taskAffinity="com.igexin.sdk.PushActivityTask"            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>        <service            android:name="com.igexin.download.DownloadService"            android:process=":pushservice"/>        <receiver            android:name="com.igexin.download.DownloadReceiver">            <intent-filter>                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>            </intent-filter>        </receiver>        <provider            android:name="com.igexin.download.DownloadProvider"            android:authorities="downloads.com.getui.demo"            android:exported="true"            android:process=":pushservice"/>        <!-- 用户自定义服务继承自GTIntentService,作为SDK与APP桥梁服务,用来接收各种消息和命令回复-->        <service android:name=".PushReceiver"/>        <!-- 用户自定义服务名 -->        <service            android:name=".DemoPushService"            android:exported="true"            android:label="PushService"            android:process=":pushservice">        </service>    </application></manifest>

之前的一下几个在配置清单里面不再需要了,因为以前的配置需要两个jar包
这里写图片描述
现在只要一个jar包就可以可以了,但是还要注意的是,
这里写图片描述

com.igexin.sdk.PushServiceUser com.igexin.sdk.PushManagerReceivercom.igexin.getuiext.activity.GetuiExtActivity com.igexin.getuiext.service.PayloadReceivercom.igexin.getuiext.service.GetuiExtService

还有就是
这里写图片描述
如果想要混淆代码还要做如下配置
这里写图片描述
基本设置完毕了,来看下成功打印信息
这里写图片描述
这里写图片描述
如有疑问请留言交流

2 0
原创粉丝点击