极光推送自动集成

配置:
第一步、Gradle配置(Module的build.gradle)
首先,确保你的 minSdkVersion在15及以上版本,然后defaultConfig中加入下面的代码(AppKey需要换成你的应用哦):

ndk {
            //选择要添加的对应cpu类型的.so库。
            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64'
            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
        }

        manifestPlaceholders = [
                JPUSH_PKGNAME: applicationId,
                JPUSH_APPKEY : "22d70ddc1524304f35f32dd8", //JPush上注册的包名对应的appkey(*换成你的*)
                JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
        ]

第二步、添加依赖
在Module的build.gradle文件中添加:
(两句代码都不能少!!!)

    //极光推送
    implementation 'cn.jiguang.sdk:jpush:3.1.5'
    implementation 'cn.jiguang.sdk:jcore:1.2.3'

如果报错的话,需要在Project的gradle.properties文件中添加下面的代码:

android.useDeprecatedNdk=true

第三步、混淆
在ProGuard文件(即proguard-rules.pro)中加入混淆代码:

-dontoptimize
-dontpreverify

-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }

第四步、初始化
新建一个MyApplication类(继承至Application),并在清单文件(AndroidManifest.xml)的application节点添加name属性关联MyApplication。在MyApplication的onCreate方法中加入初始化的代码:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化sdk
        JPushInterface.setDebugMode(true);//正式版的时候设置false,关闭调试
        JPushInterface.init(this);
        //添加tag标签,发送消息的之后就可以指定tag标签来发送了
        //Set set = new HashSet<>();
        //set.add("andfixdemo");//名字任意,可多添加几个
        //JPushInterface.setTags(this, set, null);//设置标签
    }
}

第五步、创建自定义广播接收器
极光推送是需要自定义一个广播器来接收推送消息的。下面是官方demo的MyReceiver。

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver,则:
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        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));
            processCustomMessage(context, bundle);
        
        } 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, 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())) {
            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());
        }
    }

    // 打印所有的 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 (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator it =  json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        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();
    }
    
    //send msg to MainActivity
    private void processCustomMessage(Context context, Bundle bundle) {
//      if (MainActivity.isForeground) {
//          String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//          String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//          Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
//          msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
//          if (!ExampleUtil.isEmpty(extras)) {
//              try {
//                  JSONObject extraJson = new JSONObject(extras);
//                  if (null != extraJson && extraJson.length() > 0) {
//                      msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
//                  }
//              } catch (JSONException e) {
//
//              }
//
//          }
//          context.sendBroadcast(msgIntent);
//      }
    }
}

在清单文件中配置receiver,注意修改category的name值为项目的包名。


            
                 
                 
                 
                 
                 
                
                
            
        

第六步、测试
1、进入极光推送管理后台,选择当前应用,推送,发送通知,填下推送的内容,选择目标人群,点击发送即可。


image.png

自动集成不需要在AndroidManifest中添加权限和Required那些配置,只需要配置上述即可

出处http://blog.csdn.net/qq137722697。 https://blog.csdn.net/qq137722697/article/details/52475605

你可能感兴趣的:(极光推送自动集成)