zoukankan      html  css  js  c++  java
  • 极光推送SDK集成

    极光推送SDK

    1、注册

    在官网注册,并创建安卓应用,然后根据提示下载SDK包,内含SDK和Demo,可以导入尝试,如何导入sdk包中有md文档

    2、集成到安卓中

    1. 在模块下的build.grade中加入如下配置:

      apply plugin: 'com.android.application'
      
      android {
          compileSdkVersion 29
          buildToolsVersion "29.0.3"
      
          defaultConfig {
              applicationId "你得应用包名"
              minSdkVersion 16
              targetSdkVersion 29
              versionCode 1
              versionName "1.0"
      
              testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
      
              manifestPlaceholders = [
                      JPUSH_PKGNAME : applicationId,
                      JPUSH_APPKEY : "你申请的appkey", //JPush 上注册的包名对应的 Appkey.
                      JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
              ]
          }
      
          buildTypes {
              release {
                  minifyEnabled false
                  proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
              }
          }
      
      }
      
      dependencies {
       	
      	// 此处加入依赖
          compile 'cn.jiguang.sdk:jpush:3.8.6'  // 此处以JPush 3.8.6 版本为例。
          compile 'cn.jiguang.sdk:jcore:2.5.5'  // 此处以JCore 2.5.5 版本为例。
      }
      
      

    3、创建InitApplication

    继承于Application

    package com.liqitech.parking;
    
    import android.app.Application;
    import android.util.Log;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import cn.jpush.android.api.JPushInterface;
    import cn.jpush.android.api.TagAliasCallback;
    
    public class InitApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            Log.e("xxx", "开始初始化!");
            /*开启debug*/
            JPushInterface.setDebugMode(true);
            JPushInterface.init(this);
            // 如果不想设置别名的话就不调用这个方法
            initJgAlias();
        }
    
        /**
         * 程序停止,取消连接
         */
        @Override
        public void onTerminate() {
            // 在这里可以断开连接
            super.onTerminate();
        }
    
        /**
         * 初始化极光推送的别名
         */
        public void initJgAlias() {
            //在jpush上设置别名,第一个参数就是applicationcontext,第二个随意,别重复就行,第三个就是你的别名
            JPushInterface.setAlias(this.getApplicationContext(), 57, "testAlias");
        }
    }
    

    4、创建Receiver

    package com.liqitech.parking;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import cn.jpush.android.api.CmdMessage;
    import cn.jpush.android.api.CustomMessage;
    import cn.jpush.android.api.JPushInterface;
    import cn.jpush.android.api.JPushMessage;
    import cn.jpush.android.api.NotificationMessage;
    import cn.jpush.android.service.JPushMessageReceiver;
    
    public class PushMessageReceiver extends JPushMessageReceiver {
        private static final String TAG = "PushMessageReceiver";
    
        @Override
        public void onMessage(Context context, CustomMessage customMessage) {
            Log.e(TAG, "[onMessage] " + customMessage);
            processCustomMessage(context, customMessage);
        }
    
        @Override
        public void onNotifyMessageArrived(Context context, NotificationMessage message) {
            Log.e(TAG, "[onNotifyMessageArrived] " + message);
        }
    
        @Override
        public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
            Log.e(TAG, "[onNotifyMessageDismiss] " + message);
        }
    
        @Override
        public void onRegister(Context context, String registrationId) {
            Log.e(TAG, "[onRegister] " + registrationId);
        }
    
        @Override
        public void onConnected(Context context, boolean isConnected) {
            Log.e(TAG, "[onConnected] " + isConnected);
        }
    
        @Override
        public void onCommandResult(Context context, CmdMessage cmdMessage) {
            Log.e(TAG, "[onCommandResult] " + cmdMessage);
        }
    
        @Override
        public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
    //    TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage);
            super.onTagOperatorResult(context, jPushMessage);
            Log.e("xxx", "onTagOperatorResult");
        }
    
        @Override
        public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
            super.onCheckTagOperatorResult(context, jPushMessage);
            Log.e("xxx", "onCheckTagOperatorResult");
        }
    
        @Override
        public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
            super.onAliasOperatorResult(context, jPushMessage);
            Log.e("xxx", "onAliasOperatorResult+--->"+jPushMessage);
            if (jPushMessage.getErrorCode()==6002){
                InitApplication initApplication = (InitApplication) context.getApplicationContext();
                Log.e("xxx", "errorCode=6002,重连");
                initApplication.initJgAlias();
            }
    
        }
    
        @Override
        public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
            super.onMobileNumberOperatorResult(context, jPushMessage);
            Log.e("xxx", "onMobileNumberOperatorResult");
        }
    
        //send msg to MainActivity
        private void processCustomMessage(Context context, CustomMessage customMessage) {
       /* if (MainActivity.isForeground) {
            String message = customMessage.message;
            String extras = customMessage.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 (extraJson.length() > 0) {
                        msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
                    }
                } catch (JSONException e) {
    
                }
    
            }
            LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);*/
    //    }
            Log.e("xxx", "processCustomMessage");
        }
    
        @Override
        public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
            super.onNotificationSettingsCheck(context, isOn, source);
            Log.e(TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);
        }
    }
    

    注意onAliasOperatorResult方法中是设置别名结果的回调,如果errorcode是6002,那么就重连

    4、AndroidMainfest.xm中配置

    配置权限:

        <!-- Required -->
        <permission android:name="你的包名.permission.JPUSH_MESSAGE" android:protectionLevel="signature" />
        <uses-permission android:name="你的包名.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.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.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    

    在application节点中加入属性name,并设置为.InitApplication。

    在application节点中加入配置:

    <!-- 极光推送配置开始 -->
            <service
                android:name="cn.jpush.android.service.PushService"
                android:enabled="true"
                android:exported="false"
                android:process=":pushcore">
                <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>
    
            <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="${applicationId}"/>
                </intent-filter>
            </service>
    <!--<service android:name=".push.jg.JGService"
                android:enabled="true"
                android:exported="false"
                android:process=":pushcore">
                <intent-filter>
                    <action android:name="cn.jiguang.user.service.action" />
                </intent-filter>
            </service>-->
    
            <receiver
                android:name="cn.jpush.android.service.PushReceiver"
                android:enabled="true" >
                <intent-filter android:priority="1000">
                    <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
                    <category android:name="${applicationId}"/>
                </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 since 3.0.7 -->
            <!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播 -->
            <!-- 3.3.0开始所有事件将通过该类回调 -->
            <!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter -->
            <receiver
                android:name=".PushMessageReceiver"
                android:enabled="true"
                android:exported="false" >
                <intent-filter>
                    <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
                    <category android:name="${applicationId}" />
                </intent-filter>
            </receiver>
            <receiver android:name="cn.jpush.android.service.AlarmReceiver" />
    
            <meta-data android:name="JPUSH_CHANNEL" android:value="${JPUSH_CHANNEL}"/>
            <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" /> <!--  </>值来自开发者平台取得的AppKey-->
    
            <!-- 极光推送配置结束 -->
    

    5、在极光开发者平台中测试推送自定义消息,即可在控制台中看到提示:

    看到onMessage日志出现即为成功

  • 相关阅读:
    钩子函数和回调函数的区别
    观察者模式(Observer)和发布-订阅者模式(Publish/Subscribe)区别
    前端解决跨域问题的终极武器——Nginx反向代理
    CORS(cross-origin-resource-sharing)跨源资源共享
    Vue父子组件通讯
    js的变量——基本类型保存在栈中,引用类型保存在堆中
    python
    CentOS7 下 Zabbix3.4 源码安装
    linux配置ssh公钥认证,打通root用户的免密码输入的scp通道
    python
  • 原文地址:https://www.cnblogs.com/daihang2366/p/14272175.html
Copyright © 2011-2022 走看看