Skip to content

Commit

Permalink
Merge pull request #151 from onerain88/unity-push-plugin
Browse files Browse the repository at this point in the history
Unity push plugin
  • Loading branch information
onerain88 authored Jul 22, 2021
2 parents 8c888b6 + 7347db5 commit d71de6f
Show file tree
Hide file tree
Showing 138 changed files with 7,146 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,46 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build --configuration Release

# 打包库
- run: bash ./script/package.sh

# 打包 Unity 推送插件
# 打包不包含 Gradle 配置的插件
- run: |
cd ./Push/Push.Unity
echo "Assets/LeanCloud.meta" > metaList
find ./Assets/LeanCloud/ -name \*.meta >> metaList
- uses: pCYSl5EDgo/create-unitypackage@master
with:
project-folder: ./Push/Push.Unity
package-path: 'release/unity-push-without-gradle.unitypackage'
include-files: ./Push/Push.Unity/metaList

# 打包包含 Gradle 配置的插件
- run: |
cd ./Push/Push.Unity
echo "Assets/Plugins.meta" >> metaList
find Assets/Plugins/ -name \*.meta >> metaList
- uses: pCYSl5EDgo/create-unitypackage@master
with:
project-folder: ./Push/Push.Unity
package-path: 'release/unity-push.unitypackage'
include-files: ./Push/Push.Unity/metaList

# 发布到 Releases
- name: Release
uses: fnkr/github-action-ghr@v1
if: startsWith(github.ref, 'refs/tags/')
env:
GHR_PATH: release/
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# 生成 API 文档
- name: Doc
uses: mattnotmitt/doxygen-action@v1

# 发布 API 文档
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,8 @@ Doc/
Push/Push.Unity/.vsconfig
Push/Push.Unity/Library/
Push/Push.Unity/Temp/
Push/Push.Unity/AndroidProj/
Push/Push.Unity/iOSProj/
Push/Push.Unity/Logs/
!Push/Push.Unity/Assets/**/*.meta
.vscode/
715 changes: 715 additions & 0 deletions Push/Push.Unity/Assembly-CSharp-Editor.csproj

Large diffs are not rendered by default.

685 changes: 685 additions & 0 deletions Push/Push.Unity/Assembly-CSharp.csproj

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Push/Push.Unity/Assets/LeanCloud.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Push/Push.Unity/Assets/LeanCloud/Push.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Push/Push.Unity/Assets/LeanCloud/Push/Android.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Push/Push.Unity/Assets/LeanCloud/Push/Android/Common.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.leancloud.push;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.unity3d.player.UnityPlayer;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;

public class Utils {
public final static String TAG = "LCPush";

private final static String PUSH_BRIDGE = "__LC_PUSH_BRIDGE__";
private final static String ON_REGISTER_PUSH = "OnRegisterPush";

public static IntentParser intentParser = null;

public static boolean isNullOrEmpty(String str) {
return str == null || str.length() == 0;
}

public static String getMetaString(@NonNull Context context, String key) {
String pkgName = context.getPackageName();
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(pkgName, PackageManager.GET_META_DATA);
if (appInfo.metaData.containsKey(key)) {
Object val = appInfo.metaData.get(key);
return (String) val;
}
return null;
} catch (PackageManager.NameNotFoundException e) {
Log.e(Utils.TAG, e.toString());
return null;
}
}

public static void putMetaString(@NonNull Context context, String key, String value) {
String pkgName = context.getPackageName();
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(pkgName, PackageManager.GET_META_DATA);
appInfo.metaData.putString(key, value);
} catch (Exception e) {
Log.e(Utils.TAG, e.toString());
}
}

public static void sendDeviceInfo(String vendor, String regId) {
Map<String, Object> deviceInfo = new HashMap<>();
deviceInfo.put("deviceType", "android");
deviceInfo.put("vendor", vendor);
deviceInfo.put("registrationId", regId);
// 这里先每次生成一个 installationId,一是目前不会用 installationId;二是建立长连接后,这个值刷新似乎没什么影响
UUID uuid = UUID.randomUUID();
deviceInfo.put("installationId", uuid);
TimeZone tz = TimeZone.getDefault();
deviceInfo.put("timeZone", tz.getID());
String json = (new JSONObject(deviceInfo)).toString();
UnityPlayer.UnitySendMessage(PUSH_BRIDGE, ON_REGISTER_PUSH, json);
}

public static String getLaunchData() {
Intent intent = UnityPlayer.currentActivity.getIntent();
if (intent == null) {
return null;
}

if (intentParser != null) {
return intentParser.Parse();
}

if (intent.hasExtra("content")) {
return intent.getStringExtra("content");
}

return null;
}

/**
* 用来解析 vivo, oppo 的通知数据
*/
public static class IntentParser {
public String Parse() {
Intent intent = UnityPlayer.currentActivity.getIntent();
if (intent == null) {
return null;
}

Bundle bundle = intent.getExtras();
if (bundle == null) {
return null;
}

Map<String, Object> pushData = new HashMap<>();
for (String key : bundle.keySet()) {
Log.i(Utils.TAG, key);
pushData.put(key, bundle.get(key));
}
Log.i(TAG, (new JSONObject(pushData)).toString());
return (new JSONObject(pushData)).toString();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Push/Push.Unity/Assets/LeanCloud/Push/Android/HuaWei.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityEngine;

namespace LeanCloud.Push {
public class LCHuaWeiPushManager {
public static void RegisterHuaWeiPush() {
AndroidJavaClass pushManagerClazz = new AndroidJavaClass("com.leancloud.push.huawei.HuaWeiPushManager");
pushManagerClazz.CallStatic("registerHuaWeiPush");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.leancloud.push.huawei;

import android.util.Log;

import com.huawei.agconnect.config.AGConnectServicesConfig;
import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.push.HmsMessaging;
import com.leancloud.push.Utils;
import com.unity3d.player.UnityPlayer;

public class HuaWeiPushManager {
public static void registerHuaWeiPush() {
new Thread(new Runnable() {
@Override
public void run() {
try {
String appId = AGConnectServicesConfig.fromContext(UnityPlayer.currentActivity).getString("client/app_id");
Log.i(Utils.TAG, "app id: " + appId);
String regId = HmsInstanceId.getInstance(UnityPlayer.currentActivity).getToken(appId, HmsMessaging.DEFAULT_TOKEN_SCOPE);
Utils.sendDeviceInfo("HMS", regId);

HmsMessaging.getInstance(UnityPlayer.currentActivity).turnOnPush().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
Log.i(Utils.TAG, "turn on successfully");
} else {
Log.i(Utils.TAG, "turn on failed");
}
}
});
} catch (ApiException e) {
e.printStackTrace();
}
}
}).start();
}
}
Loading

0 comments on commit d71de6f

Please sign in to comment.