I'm making a game in 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
This is my manifest 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 i.e. -1. Also title and message are null. Even though the notification has the title and message parameter.
I have seen lots of examples for Android Studio but not for 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.
Related
Hi trying to send SMS with android using below code.
public static void sendsmsstd(String number, String message, Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", number);
intent.putExtra("sms-body", message);
context.startActivity(intent);
}
Many sites have suggested it , but when code runs and SMS app opens, it choose right contact to send message but message body is always empty.
What else can be done to fix it. I m missing some code.
You're using sms-body. Change that to sms_body and that should fix it.
Check if an app, for example, Instagram is started by user.
Note: My app is targeting lollipop and above versions in android
Yeah the only way you can do it is through the Accessibility Service. Look at this page to understand how to create it. https://developer.android.com/training/accessibility/service.html They will also need to enable the service via the services -> accessibility screen.
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED you can probably interrogate the package in front to figure out if Instigram is on top.
You definitely don't want to use getRunningTasks since the function was modified in Android 5.0+
I figured out that I can do this by using usage access feature.
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static String getForegroundProcess(Context context) {
String topPackageName = null;
UsageStatsManager usage = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> stats = usage.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1000, time);
if (stats != null) {
SortedMap<Long, UsageStats> runningTask = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats) {
runningTask.put(usageStats.getLastTimeUsed(), usageStats);
}
if (runningTask.isEmpty()) {
return null;
}
topPackageName = runningTask.get(runningTask.lastKey()).getPackageName();
}
if(topPackageName==null) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
context.startActivity(intent);
}
return topPackageName;
}
Now continuously check if the desired app is in the foreground.
String fg=getForegroundProcess(getApplicationContext());
if(fg != null && fg.contains("com.instagram.android")){
//Instagram is in foreground
}else {
}
I continuously run the above code with a job service.Which is available for
lollipop and above.
I am making one app to allow user to share all video from any sharing app. My issue is file attached successfully but the file content is not attaching. below is my full source code. Let me know where I am making a mistake.
private void shareVideo() {
Intent localIntent = new Intent("android.intent.action.SEND");
Uri localUri = Uri.fromFile(new File(CorporateAdaptor.this.rawVideoId + ".mp4"));
String str = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(localUri.toString()));
localIntent.setType(str);
localIntent.setAction("android.intent.action.SEND");
if (str == null) {
str = "*/*";
}
localIntent.setType(str);
localIntent.putExtra("android.intent.extra.STREAM", localUri);
CorporateAdaptor.this.mContext.startActivity(Intent.createChooser(local Intent, "Where you want to share?"));
}
This is my code that was I am using when user click on the share button. It will open a share dialog and when I select the gmail app, the file is attached but it's showing me a toast message "Couldn't find attachment". And when I click on send email file was not sending.
I'm trying to send a notification to my pebble watch. I'm using this code, which is basically the example from the website:
public void sendPebble(String title, String body) {
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
final Map<String, String> data = new HashMap<String, String>();
data.put("title", title);
data.put("body", body);
final JSONObject jsonData = new JSONObject(data);
final String notificationData = new JSONArray().put(jsonData).toString();
i.putExtra("messageType", "PEBBLE_ALERT");
i.putExtra("sender", "Test");
i.putExtra("notificationData", notificationData);
Log.d("Test", "Sending to Pebble: " + notificationData);
sendBroadcast(i);
}
I'm getting the message in LogCat, but no notification on the watch. The procedure seems simple enough, is there something too obvious I missed? Or is the documentation just incomplete?
Edit: The obvious questions:
Yes, the watch is connected
Yes, I have third party notifications enabled
Okay, this was my problem:
By default the Pebble app only sends notifications to the watch while the screen of the phone is off. For development I always have the screen active while the phone is connected via USB. So, the solution was: Enable the "Always send Notifications" option in the Pebble app.
Maybe this spares someone else a headache.
I downloaded apk file from url(my server) and save it in sdcard. If user install it from sdcard, I want to know, whether is any notification that app is installed successfully or is app istalled in device. Is there any callback on installed app
try this code :
protected boolean isAppInstalled(String packageName) {
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (mIntent != null) {
return true;
}
else {
return false;
}
}
to get the package name of the app easily : just search your app in the google play website , and then you will take the id parameter ( it is the package name of the app) . Example :
you will search on Youtube app on google play , and you will find it in this url :
https://play.google.com/store/apps/details?id=com.google.android.youtube&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5nb29nbGUuYW5kcm9pZC55b3V0dWJlIl0.
the package name is the id param, so it is : com.google.android.youtube
And then when you want to test , you will just have :
String packageName = "com.google.android.youtube";
boolean isYoutubeInstalled = isAppInstalled(packageName);
PLUS : if you want to get the list of all installed apps in you device , you can find your answer in this tutorial
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
You'll get the list of all installed applications on Android.
Use this to check if an application is installed
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> list = pm.getInstalledApplications(0);
for (int i = 0; i < list.size(); i++) {
if(list.get(i).packageName.equals("com.my package")){
//do what you want
}
}
In Youtube Player API, you can access YoutubeIntents class and use isYoutubeInstalled to verify if device has the Android app or not.