学习笔记-极光推送使用心得

来源:互联网 发布:python和php的区别 编辑:程序博客网 时间:2024/06/08 15:34
       最近赶项目,项目组两个人短期内完成三个关联项目,碰巧用到极光推送,之前知道这个东西很有名,一直没机会接触,趁这次集成到项目中,将集成过程中遇到的问题总结分享一下.

      极光推送集成方式有两种,一种是手动集成,比较繁琐,不推荐使用,当然要是你IDE是eclipse,那只能手动辛苦一下了,这里我使用的是 jcenter集成,方式,总体感觉还是很方便的,过程如下:

1.moudle gradle defaultconfig 里面配置如下:

ndk {            //选择要添加的对应cpu类型的.so库。            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a','x86', 'x86_64', 'mips', 'mips64'            // 还可以添加        }        manifestPlaceholders = [                JPUSH_PKGNAME : applicationId,                JPUSH_APPKEY : "---------------", //JPush上注册的包名对应的appkey.                JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.        ]    }dependencies{compile 'cn.jiguang.sdk:jpush:3.0.3'  // 此处以JPush 3.0.3 版本为例。    compile 'cn.jiguang.sdk:jcore:1.1.1'  // 此处以JCore 1.1.1 版本为例。}
2.manifiest.xml 配置如下(虽然官网上说用 jcenter集成,manifest 文件不需要配置,但实际有些必须配置).    

         权限配置:  

<!--极光推送-->    <!-- Required  一些系统要求的权限,如访问网络等 -->    <uses-permission android:name="com.shanlin.autostore.terminal.permission.JPUSH_MESSAGE" />    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WAKE_LOCK" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_SETTINGS" />    <uses-permission android:name="android.permission.VIBRATE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <!-- Optional for location -->    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 -->    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />    <uses-permission android:name="android.permission.GET_TASKS" />

   自定义receiver, 如果你需要在接收到推送通知去做一些操作,需要自定义一个receiver;

<receiver android:name=".jpush.MyReceiver"            android:enabled="true">                <!-- 这里是开发者自己定义的广播接收器,名字自己定-->                <intent-filter>                    <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required  用户注册SDK的intent-->                    <action android:name="cn.jpush.android.intent.UNREGISTRATION" />                    <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 说明这个功能是从1.6.3版本才开始有的,之前所用的版本是获取不到连接状态的变化的,我目前所用的版本是1.7.2,所以可以获取连接状态的变化的 -->                    <category android:name="com.shanlin.autostore.terminal" />                </intent-filter>        </receiver>meta-data<meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>        <!-- Required. AppKey copied from Portal -->        <meta-data android:name="JPUSH_APPKEY" android:value="-------你的app-key"/>

MyReceiver.java

import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import com.shanlin.autostore.terminal.activity.TestActivity;import org.json.JSONException;import org.json.JSONObject;import java.util.Iterator;import cn.jpush.android.api.JPushInterface;/** * 自定义接收器 *  * 如果不定义这个 Receiver,则: * 1) 默认用户会打开主界面 * 2) 接收不到自定义消息 */public class MyReceiver extends BroadcastReceiver {private static final String TAG = "JIGUANG-Example";@Overridepublic void onReceive(Context context, Intent intent) {try {Bundle bundle = intent.getExtras();Logger.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);Logger.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);//send the Registration Id to your server...} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {Logger.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));Log.d("wr", "wr**********************");} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知");int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {Logger.d(TAG, "[MyReceiver] 用户点击打开了通知");//打开自定义的ActivityIntent i = new Intent(context, TestActivity.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())) {Logger.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);Logger.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);} else {Logger.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))) {Logger.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().toString();sb.append("\nkey:" + key + ", value: [" +myKey + " - " +json.optString(myKey) + "]");}} catch (JSONException e) {Logger.e(TAG, "Get message extra JSON error!");}} else {sb.append("\nkey:" + key + ", value:" + bundle.getString(key));}}return sb.toString();}}

还需要自定义一个application;

import android.app.Application;import cn.jpush.android.api.JPushInterface;/** * Created by DELL on 2017/7/22 0022. */public class TerminalApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        JPushInterface.setDebugMode(true);        JPushInterface.init(this);    }}

大工告成!

原创粉丝点击