安卓集成极光推送SDK

来源:互联网 发布:网络美女排行榜2016 编辑:程序博客网 时间:2024/05/17 02:38

在云计算飞速发展的今天,很多较复杂的功能已经不用开发者去摸索研究。直接在工程中集成别的公司开发的SDK。

集成极光推送SDK,完成安卓端手机推送

第一步,注册,添加应用,获得AppKey

第二步,新建项目


第三步,下载安卓端SDK,根据集成指南完成对SDK的集成

文档链接

第四步,测试

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        JPushInterface.setDebugMode(true);        JPushInterface.init(this);    }}
控制台发送消息

第五步,自定义Receiver对推送进行处理

Manifest

<!-- User defined.  For test only  用户自定义的广播接收器 -->        <receiver            android:name=".MyReceiver"            android:enabled="true"            android:exported="false">            <intent-filter>                <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required  用户注册SDK的intent -->                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required  用户接收SDK消息的intent -->                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required  用户接收SDK通知栏信息的intent -->                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required  用户打开自定义通知栏的intent -->                <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->                <category android:name="com.example.konghao.jpushtest1" />            </intent-filter>        </receiver>
MyReceiver

public class MyReceiver extends BroadcastReceiver {    private static String TAG = "MyReceiver";    @Override    public void onReceive(Context context, Intent intent) {        try {            Bundle bundle = intent.getExtras();            Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);                Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);                //send the Registration Id to your server...            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {                Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {                Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);                Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {                Log.d(TAG, "[MyReceiver] 用户点击打开了通知");                //打开自定义的Activity                Intent i = new Intent(context, MessageActivity.class);                i.putExtras(bundle);                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );                context.startActivity(i);            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {                Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));                //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..            } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);                Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);            } else {                Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());            }        } catch (Exception e){        }    }    // 打印所有的 intent extra 数据    private static String printBundle(Bundle bundle) {        StringBuilder sb = new StringBuilder();        for (String key : bundle.keySet()) {            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));            }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {                    Log.i(TAG, "This message has no Extra data");                    continue;                }                try {                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));                    Iterator<String> it =  json.keys();                    while (it.hasNext()) {                        String myKey = it.next();                        sb.append("\nkey:" + key + ", value: [" +                                myKey + " - " +json.optString(myKey) + "]");                    }                } catch (JSONException e) {                    Log.e(TAG, "Get message extra JSON error!");                }            } else {                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));            }        }        return sb.toString();    }}
第六步、分组发送推送,以别名为例

摘自文档:

Method - setAlias

调用此 API 来设置别名。

需要理解的是,这个接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。

支持的版本

开始支持的版本:3.0.7

接口定义

public static void setAlias(Context context, int sequence, String alias);

参数定义

  • sequence

    • 用户自定义的操作序列号, 同操作结果一起返回,用来标识一次操作的唯一性。
  • alias

    • 每次调用设置有效的别名,覆盖之前的设置。
    • 有效的别名组成:字母(区分大小写)、数字、下划线、汉字、特殊字符@!#$&*+=.|。
    • 限制:alias 命名长度限制为 40 字节。(判断长度需采用UTF-8编码)

设置别名:

JPushInterface.setAlias(this, 1000, "test");