极光推送自动集成

配置:
第一步、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<String> 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<String> 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值为项目的包名。

<receiver
            android:name=".ui.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.ACTION_RICHPUSH_CALLBACK" /> <!--Optional 用户接受Rich Push Javascript 回调函数的intent-->
                <action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
                <category android:name="cn.figo.dingding.edu" /><!-- name为当前项目的包名 -->
            </intent-filter>
        </receiver>

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


image.png

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

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,268评论 25 708
  • 【快速搞定】2分钟搞定极光推送(极光推送Android端集成) 一、前言 2分钟只是一个虚数哈,不过只要你速度快,...
    xiefy阅读 1,910评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • iOS autoLayout约束下,给UITableViewCell内部的UIView添加带圆角虚线框
    姬歌阅读 920评论 0 2
  • “我不会再结婚了”,何洁这句话,相信这几天看相关微信或者是看过采访视频的人印象都非常深,且何洁的表情让人...
    4点半的恩赐阅读 332评论 0 0