Connect Unity to SDK - Jiguang Push

2021.09.09 record, 2023 release, if there is anything wrong, please include it. post late

If you want to see Android's native access to JPush - SDK, move to Android's native integration of JPush SDK_jpush android sdk v4.7.2

Jiguang Push- Access

Version:

  •     Unity 2020.3.10f1
  •     JPush - Unity  3.2.0

Official plugin: GitHub - jpush/jpush-unity3d-plugin: JPush's officially supported Unity3d plugin (Android & iOS). JPush's officially supported Unity3d plugin (Android & iOS).

Download the Zip archive and decompress it, then copy the Plugins file and its sub-files to Unity's Assets:

Do one thing first, open our AndroidManifest file, then change the content of the file, replace all the package names with the project package name , and then replace the APPKEY in it with the APPKEY value of the corresponding software applied for on the official website .

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="包名">
  <!-- Required -->
  <permission
      android:name="包名.permission.JPUSH_MESSAGE"
      android:protectionLevel="signature" />
  <!-- Required  一些系统要求的权限,如访问网络等 -->
  <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.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <!-- Optional for location -->
  <uses-permission android:name="android.permission.VIBRATE" />
  <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.CHANGE_WIFI_STATE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  <!-- Android Q后台定位权限 -->
  <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" />

  <uses-feature android:glEsVersion="0x00030000" />
  <uses-feature
      android:name="android.hardware.vulkan.version"
      android:required="false" />
  <uses-feature
      android:name="android.hardware.touchscreen"
      android:required="false" />
  <uses-feature
      android:name="android.hardware.touchscreen.multitouch"
      android:required="false" />
  <uses-feature
      android:name="android.hardware.touchscreen.multitouch.distinct"
      android:required="false" />

  <application
      android:allowBackup="false"
      android:label="@string/app_name">
    <activity android:name="包名.MainActivity"></activity>
    <!-- For test only. Demo 的主程序 start -->
    <activity
        android:name="cn.jiguang.unity.push.UnityPluginActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

      <meta-data
          android:name="android.notch_support"
          android:value="true" />
    </activity>
    <!-- For test only. 测试的主程序 end -->
    <!-- Rich push 核心功能 since 2.0.6 -->
    <activity
        android:name="cn.jpush.android.ui.PopWinActivity"
        android:exported="false"
        android:theme="@style/MyDialogStyle"></activity>
    <!-- Required SDK核心功能 -->
    <activity
        android:name="cn.jpush.android.ui.PushActivity"
        android:configChanges="orientation|keyboardHidden"
        android:exported="false"
        android:theme="@android:style/Theme.NoTitleBar">
      <intent-filter>
        <action android:name="cn.jpush.android.ui.PushActivity" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="包名" />
      </intent-filter>
    </activity>
    <!-- Required SDK 核心功能 -->
    <!-- 可配置android:process参数将PushService放在其他进程中 -->
    <service
        android:name="cn.jpush.android.service.PushService"
        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>
    <!-- since 3.0.9 Required SDK 核心功能 -->
    <provider
        android:name="cn.jpush.android.service.DataProvider"
        android:authorities="包名.DataProvider"
        android:exported="false"
        android:process=":pushcore" />
    <!-- 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="包名" />
      </intent-filter>
    </service>
    <!-- since 3.1.0 Required SDK 核心功能 -->
    <provider
        android:name="cn.jpush.android.service.DownloadProvider"
        android:authorities="包名.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="包名" />
      </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" />
    <!-- since 3.3.0 接收JPush相关事件 -->
    <receiver android:name="cn.jiguang.unity.push.JPushEventReceiver">
      <intent-filter>
        <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />

        <category android:name="包名"></category>
      </intent-filter>
    </receiver>
    <!-- since 3.3.0 Required SDK核心功能 -->
    <activity
        android:name="cn.jpush.android.service.JNotifyActivity"
        android:exported="true"
        android:taskAffinity="jpush.custom"
        android:theme="@android:style/Theme.Translucent.NoTitleBar">
      <intent-filter>
        <action android:name="cn.jpush.android.intent.JNotifyActivity" />

        <category android:name="包名" />
      </intent-filter>
    </activity>
    <!-- since 3.3.0 Required SDK 核心功能 -->
    <!-- 可配置android:process参数将PushService放在其他进程中 -->
    <!-- User defined.  For test only 继承自cn.jpush.android.service.JCommonService 
-->
    <service
        android:name="cn.jiguang.unity.push.PushService"
        android:process=":pushcore">
      <intent-filter>
        <action android:name="cn.jiguang.user.service.action" />
      </intent-filter>
    </service>
    <!-- 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="这里是JPush平台申请的APPKEY" />
    <!-- </>值来自开发者平台取得的AppKey -->
    <meta-data
        android:name="unity.splash-mode"
        android:value="0" />
    <meta-data
        android:name="unity.splash-enable"
        android:value="True" />
    <meta-data
        android:name="android.max_aspect"
        android:value="2.1" />
    <meta-data
        android:name="notch.config"
        android:value="portrait|landscape" />
    <meta-data
        android:name="unity.build-id"
        android:value="380dd5c3-e02d-40fc-babf-8ecc0e24faf6" />
  </application>

</manifest>

There is another pit to record, that is, Unity needs to set the AndroidManifest to the custom mode, and then select the file we changed, otherwise after the package is started, JPush will prompt various questions. In summary, there is one problem: the package name is inconsistent.

 

So far, JPushSKD related content has been put into Unity


Next we have to start writing code and registering events:

The Push official website recommends that we enable the debug mode before Init during the development phase , so we call JPushBinding.SetDebug(true);

 Then we initialize, the parameter is an object with a unique name in the game scene, which is used to receive the parameters passed by JPush .

Then we go to JPush official website to start pushing messages

Students who can interact with Unity and native will know that native will find the function through the corresponding object name we sent, and after the function is triggered, call the corresponding function under the Unity related object again.

using JPush;
using UnityEngine;

public class Routation : MonoBehaviour
{
    void Start()
    {
        JPushBinding.SetDebug(true);
        JPushBinding.Init(this.name);
    }
    // 开发者自己处理由 JPush 推送下来的消息。
    void OnReceiveMessage(string jsonStr)
    {
        Debuger.Log("recv----message-----" + jsonStr);
    }

    /** 接收到的内容格式为如下内容:
    *  {
    *      "msgid":"xxxxxxxxxxxxxxxx"
    *      "extras":
    *      {      
    *          "key1": "value1"
    *          "key2": "value2"
    *      }
    *      "title":"相关标题"
    *      "content":"相关内容"
    *  }      
    *  
    *  我们常用到的函数为:
    *   OnReceiveNotification
    *   OnOpenNotification
    *   
    *   在一些高级应用中,我们会触发其他函数:
    *   OnJPushTagOperateResult
    *   OnJPushAliasOperateResult
    *   OnGetRegistrationId
    *   OnMobileNumberOperatorResult
    */
    void OnReceiveNotification(string jsonStr)
    {
        Debuger.Log("recv---notification---" + jsonStr);
    }

    void OnOpenNotification(string jsonStr)
    {
        Debuger.Log("recv---openNotification---" + jsonStr);
    }

    /// <summary>
    /// JPush 的 tag 操作回调。
    /// </summary>
    /// <param name="result">操作结果,为 json 字符串。</param>
    void OnJPushTagOperateResult(string result)
    {
        Debuger.Log("JPush tag operate result: " + result);
    }

    /// <summary>
    /// JPush 的 alias 操作回调。
    /// </summary>
    /// <param name="result">操作结果,为 json 字符串。</param>
    void OnJPushAliasOperateResult(string result)
    {
        Debuger.Log("JPush alias operate result: " + result);
    }

    void OnGetRegistrationId(string result)
    {
        Debuger.Log("JPush on get registration Id: " + result);
    }

    void OnMobileNumberOperatorResult(string result)
    {
        Debuger.Log("JPush On Mobile Number Operator Result: " + result);
    }
}

Then package and publish, and then check the results.


Guess you like

Origin blog.csdn.net/Wenhao_China/article/details/130193294