极光推送集成过程

作者: _Sisyphus | 来源:发表于2017-05-05 10:39 被阅读129次

    jcenter 自动集成步骤

    好处:不需要在项目中添加jar和so,jcenter会自动完成依赖;简化了之前繁琐的配置步骤

    1. Module的build.gradle

        android {
            ......
            defaultConfig {
                applicationId "com.xxx.xxx" //JPush上注册的包名.
                ......
        
                ndk {
                    //选择要添加的对应cpu类型的.so库。 
                    abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a' 
                    // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
                }
        
                manifestPlaceholders = [
                    JPUSH_PKGNAME : applicationId,
                    JPUSH_APPKEY : "你的appkey",     //JPush上注册的包名对应的appkey.
                    JPUSH_CHANNEL : "自定义渠道名称", //用户渠道统计的渠道名称
                ]
                ......
            }
            ......
        }
        
        dependencies {
            ......
        
            compile 'cn.jiguang.sdk:jpush:3.0.5'  // 此处以JPush 3.0.5 版本为例。
            compile 'cn.jiguang.sdk:jcore:1.1.2'  // 此处以JCore 1.1.2 版本为例。
            ......
        }
    

    注 : 如果在添加以上 abiFilter 配置之后android Studio出现以下提示:

           NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
    

    则在 Project 根目录的gradle.properties文件中添加:

    android.useDeprecatedNdk=true
    

    说明:若没有res/drawable-xxxx/jpush_notification_icon这个资源默认使用应用图标作为通知icon,在5.0以上系统将应用图标作为statusbar icon可能显示不正常,用户可定义没有阴影和渐变色的icon替换这个文件,文件名不要变。

    这里建议将应用icon设置为通知显示图标,这里设置有点问题,我尝试在初始化之后调用以下代码设置失败。

     BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
     builder.statusBarDrawable = R.drawable.jpush_notification_icon;
    

    2. 初始化sdk

        JPushInterface.setDebugMode(true);//正式版的时候设置false,关闭调试
        JPushInterface.init(this);
    

    3. 混淆

    -dontoptimize
    -dontpreverify
    
    -dontwarn cn.jpush.**
    -keep class cn.jpush.** { *; }
    
    -dontwarn cn.jiguang.**
    -keep class cn.jiguang.** { *; }
    

    4. 创建自定义广播接收器

    JPush SDK 收到推送,通过广播的方式,转发给开发者App,我们可以自定义广播接受到推送消息。

    /**
     * 自定义接收器
     * 
     * 如果不定义这个 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();
            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));
                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) {
            ...//省略了
        }
    
        //send msg to MainActivity
        private void processCustomMessage(Context context, Bundle bundle) {
            ...//省略了
        }
    }
    

    别忘了在清单文件中注册哦:

      <!-- User defined.  For test only  用户自定义的广播接收器-->
        <receiver
            android:name="com.example.jpushdemo.MyReceiver"        //自己写的Receiver
            android:exported="false"
            android:enabled="true">
            <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="${applicationId}" />                //包名
            </intent-filter>
        </receiver>
        
    到这里一般就可以正常收到推送了。
    

    5. 推送到单个用户或一组用户

    • 单个推送

      对应别名 alias:为安装了应用程序的用户,取个别名来标识。以后给该用户 Push 消息时,就可以用此别名来指定。
      每个用户只能指定一个别名。
      Method - setAlias:新的调用会覆盖之前的设置

    • 分组推送

      对应标签 tag:为安装了应用程序的用户,打上标签。其目的主要是方便开发者根据标签,来批量下发 Push 消息。
      可为每个用户打多个标签。
      Method - setTags:新的调用会覆盖之前的设置
      </br>

    • 设置别名和tag

      JPushInterface.setAliasAndTags(getApplicationContext(),alias, Tags);
      

    6. 其他:

    • 极光统计:所有的 Activity 的 onResume / onPause 方法里调用
    代码示例
    
            @Override
            protected void onResume() {
                super.onResume();
                JPushInterface.onResume(this);
            }
            @Override
            protected void onPause() {
                super.onPause();
                JPushInterface.onPause(this);
            }
    >极光推送的设备唯一性标识 RegistrationID:
    

    </br>

    RegistrationID 生成规则解析:       
    
        Android 上因为国内存在大量山寨设备的原因,正常的 IMEI, Mac Address, AndroidID 这些可以考虑用作唯一标识的值,都是不可以用的,因为这些值在一批设备中可能都是同一个值。极光的基本思路是:
        
    生成一个 DeviceID 保存到 Settings, External Storage。依赖本地存储,应用被卸载后重新安装这些存储里的 DeviceID 还在的话,就是同一个设备。这一条理论上解决 90% 的不变性问题。
            
    DeviceID 之外增加补充规则:综合根据 IMEI, MAC Address, AndroidID, Serial_Number 这几个值来判断,是否可能是老设备。

    相关文章

      网友评论

      • 碧云天丶:权限忘记了
        碧云天丶:@YOLO_Android 你是对的。
        _Sisyphus:权限不用加的,依赖里面自动加的我记得当时看他们文档。当时是测试成功了的~ 你可以具体去看看他们文档,时间长了 记得不是特别清楚了哈

      本文标题:极光推送集成过程

      本文链接:https://www.haomeiwen.com/subject/gfcdtxtx.html