-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
Unity push plugin
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
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.
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.
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(); | ||
} | ||
} |