极光推送接入

极光推送的集成

最近项目中用到了极光推送,现在分享一下自己的实现过程:

  • 第一步 上传包名到到极光平台生成key值
  • 第二步 下载推送demo(新建测试项目然后导入极光依赖包)
  • 第三步 根据demo依次添加so文件和jar包
  • 第四步 初始化极光推送
  • 第五步 自定义广播接收器
  • 第六步 配置清单文件
  • 第七步 模拟运行发送消息

第一步

点击应用管理创建你的应用
这里写图片描述
-然后找到应用包名填写你的项目包名
这里写图片描述
- 上传成功点击推送设置就可以看到AppKey
这里写图片描述

第二部

然后你可以看到你的应用包名和demo,依赖到你的测试项目中运行
这里写图片描述

第三步

**把so文件和jar包放入到相应的位置:

这里写图片描述

第四步

**对极光推送进行初始化:

public class InspectApplication extends Application {
 //极光推送的registrationID
    public static String registrationID;
    private static InspectApplication mApplication;
public static InspectApplication getApplicationInstance() {
        return mApplication;
    }

    public InspectApplication() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initData();
    }

    private void initData() {
        //极光推送
        JPushInterface.setDebugMode(false);// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);         // 初始化 JPush
        registrationID = JPushInterface.getRegistrationID(context);
        }
      }

自定义广播接收器

/**
 * JPush推送 自定义接收器
 */

public class PushReceiver extends BroadcastReceiver {
    private static final String TAG = "PushReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        //LogUtil.d(TAG, "[PushReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())){
            /**
             * SDK 向 JPush Server 注册所得到的注册 全局唯一的 ID ,可以通过此 ID 向对应的客户端发送消息和通知
             *
             * 注册JPush成功时回调,此时registrationID的值才不为空
              */

           InspectApplication.registrationID=bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            LogUtil.d(TAG,"[PushReceiver] 接收Registration Id : "+InspectApplication.registrationID);
        }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){
            /**
             * 收到了自定义消息 Push 。
             *
             * SDK 对自定义消息,只是传递,不会有任何界面上的展示。
             *
             * 如果开发者想推送自定义消息 Push,则需要在 AndroidManifest.xml 里配置此 Receiver action,并且在自己写的 BroadcastReceiver 里接收处理。
             */
            LogUtil.d(TAG, "[PushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

        }else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){

            /**
             * SDK 1.2.9 以上版本支持。
             *
             * 保存服务器推送下来的附加字段。
             *
             * 这是个 JSON 字符串。对应 API 通知内容的 extras 字段。
             *
             * 对应 Portal 推送消息界面上的“可选设置”里的附加字段。
             *
             */
            String pushData = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (!"".equals(pushData) && !"null".equals(pushData) && pushData!=null){
               // JSONObject jsonObject = new JSONObject(pushData);
                //接收到推送进行刷新
            }
        }else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){
            LogUtil.d(TAG, "[PushReceiver] 用户点击打开了通知");
            /**
             * 用户点击了通知。
             *
             * 一般情况下,用户不需要配置此 receiver action。
             *
             * 如果开发者在 AndroidManifest.xml 里未配置此 receiver action,那么,SDK 会默认打开应用程序的主 Activity,相当于用户点击桌面图标的效果。
             *
             * 如果开发者在 AndroidManifest.xml 里配置了此 receiver action,那么,当用户点击通知时,SDK 不会做动作。
             *
             * 开发者应该在自己写的 BroadcastReceiver 类里处理,比如打开某 Activity 。
             */
            //自定义activity的处理
        }

    }
}

第六步

清单文件权限配置:

 <!--极光推送权限-->
    <permission
        android:name="com.finance.geex.merchants.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.finance.geex.merchants.permission.JPUSH_MESSAGE" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- 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.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" />

清单文件:

 <!--极光推送-->
        <!-- Rich push 核心功能 since 2.0.6-->
        <activity
            android:name="cn.jpush.android.ui.PopWinActivity"
            android:theme="@style/MyDialogStyle"
            android:exported="false">
        </activity>

        <!-- Required SDK核心功能-->
        <activity
            android:name="cn.jpush.android.ui.PushActivity"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.NoTitleBar"
            android:exported="false">
            <intent-filter>
                <action android:name="cn.jpush.android.ui.PushActivity" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.finance.geex.merchants" />
            </intent-filter>
        </activity>

        <!-- Required SDK 核心功能-->
        <!-- 可配置android:process参数将PushService放在其他进程中 -->
        <service
            android:name="cn.jpush.android.service.PushService"
            android:process=":mult"
            android:exported="false">
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTER" />
                <action android:name="cn.jpush.android.intent.REPORT" />
                <action android:name="cn.jpush.android.intent.PushService" />
                <action android:name="cn.jpush.android.intent.PUSH_TIME" />
            </intent-filter>
        </service>
        <!-- since 3.0.9 Required SDK 核心功能-->
        <provider
            android:authorities="com.finance.geex.merchants.DataProvider"
            android:name="cn.jpush.android.service.DataProvider"
            android:exported="false"
            />

        <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
        <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
        <service
            android:name="cn.jpush.android.service.DaemonService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="cn.jpush.android.intent.DaemonService" />
                <category android:name="com.finance.geex.merchants" />
            </intent-filter>

        </service>
        <!-- since 3.1.0 Required SDK 核心功能-->
        <provider
            android:authorities="com.finance.geex.merchants.DownloadProvider"
            android:name="cn.jpush.android.service.DownloadProvider"
            android:exported="true"
            />
        <!-- Required SDK核心功能-->
        <receiver
            android:name="cn.jpush.android.service.PushReceiver"
            android:enabled="true"
            android:exported="false">
            <intent-filter android:priority="1000">
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />   <!--Required  显示通知栏 -->
                <category android:name="com.finance.geex.merchants" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <!-- Optional -->
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

        <!-- Required SDK核心功能-->
        <receiver android:name="cn.jpush.android.service.AlarmReceiver" android:exported="false"/>

        <!-- User defined.  For test only  用户自定义的广播接收器-->
        <receiver
            android:name=".receiver.PushReceiver"
            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="com.finance.geex.merchants" />
            </intent-filter>
        </receiver>

        <!-- Required  . Enable it you can get statistics data with channel -->
        <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
        <meta-data android:name="JPUSH_APPKEY" android:value="61b45cd46336409122211234" /> <!--  </>值来自开发者平台取得的AppKey-->
        <!--极光推送-->

第七步 模拟运行发送消息

这里写图片描述

猜你喜欢

转载自blog.csdn.net/boomlei/article/details/79729936
今日推荐