简体   繁体   English

如何知道是否通过Android上的通知启动了应用/游戏?

[英]How to know if app/game was launched via notification on Android?

I'm making a game in Unity for Android. 我正在用Unity for Android制作游戏。 I have implemented a notification system. 我已经实施了一个通知系统。 Notifications show and clicking on them opens the game. 显示通知,然后单击它们即可打开游戏。 The thing I'm stuck on is how to know if the game/app is launched by tapping the notification? 我遇到的问题是如何通过点击通知来知道是否启动了游戏/应用程序?

Here is the java code for the notification plugin I'm using https://github.com/GoShikhar/unity-android-notifications/blob/master/PluginSrc/app/src/main/java/net/agasper/unitynotification/UnityNotificationManager.java 这是我正在使用https://github.com/GoShikhar/unity-android-notifications/blob/master/PluginSrc/app/src/main/java/net/agasper/unitynotification/UnityNotificationManager的通知插件的Java代码.java

This is my manifest https://github.com/GoShikhar/unity-android-notifications/blob/master/UnityProject/Assets/Plugins/Android/AndroidManifest.xml 这是我的清单https://github.com/GoShikhar/unity-android-notifications/blob/master/UnityProject/Assets/Plugins/Android/AndroidManifest.xml

In my unity start scene I use this to check for intent messages. 在我的统一开始场景中,我用它来检查意图消息。

void Start(){
 AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 var activityObject = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
 AndroidJavaObject intent = activityObject.Call<AndroidJavaObject>("getIntent");
 int NotificationID = intent.Call<int>("getIntExtra", "id", -1);
 print("NOTIFDICATION ID " + NotificationID);
 bool hasExtra = intent.Call<bool>("hasExtra", "arguments");
 string arguments = null;
 if (hasExtra)
 {
    AndroidJavaObject extras = intent.Call<AndroidJavaObject>("getExtras");
    arguments = extras.Call<string>("getString", "title");
    print("title : " + arguments);
    arguments = extras.Call<string>("getString", "message");
    print("message: " + arguments);
 }

 if (arguments != null)
 {
        print("App opened via notification");
 }
}

This not working. 这行不通。 The default notification ID is being printed ie -1. 默认的通知ID正在打印,即-1。 Also title and message are null. 标题和消息也为空。 Even though the notification has the title and message parameter. 即使通知具有title和message参数。

I have seen lots of examples for Android Studio but not for Unity. 我看到了很多Android Studio的示例,但没有Unity的示例。 So any help will be appreciated. 因此,任何帮助将不胜感激。

Thanks in advance. 提前致谢。

Do you mean deep linking? 您是说深层链接吗? Perhaps this Github project would be helpful: https://github.com/TROPHiT/UnityDeeplinks . 也许这个Github项目会有所帮助: https : //github.com/TROPHiT/UnityDeeplinks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM