нужно получать все активные уведомления на моем смартфоне и выводить полученное через TextMeshPro в моем приложении
пожалуйста, мне нужна хоть какая-то обратная связь от более опытных пользователей... вот java класс:
package com.example.getnotificationslibrary;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.io.ByteArrayOutputStream;
public class ReceiveNotificationsPlugin extends NotificationListenerService
{
Context context;
public String title;
public String text;
@Override
public void onCreate()
{
super.onCreate();
context = getApplicationContext();
}
public void onNotificationPosted(StatusBarNotification sbn, final IReceiveNotificationsCallback callback)
{
String pack = sbn.getPackageName();
String ticker ="";
if(sbn.getNotification().tickerText !=null)
{
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;
Intent msgrcv = new Intent("Msg");
msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
title = extras.getString("android.title");
text = extras.getCharSequence("android.text").toString();
if(id != null)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
id.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
msgrcv.putExtra("icon",byteArray);
}
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}
}
вот интерфейс:
package com.example.getnotificationslibrary;
import android.service.notification.StatusBarNotification;
public interface IReceiveNotificationsCallback
{
void onNotificationPosted(StatusBarNotification sbn);
}
вот C# callback:
public interface IReceiveNotifications
{
void onNotificationPosted();
}
public class IReceiveNotificationsCallback : AndroidJavaProxy, IReceiveNotifications
{
public event System.Action OnNotificationPosted;
public IReceiveNotificationsCallback() :
base("com.example.getnotificationslibrary.IReceiveNotificationsCallback") { }
public void onNotificationPosted()
{
OnNotificationPosted?.Invoke();
}
}
вот тут я вызываю java функцию и присваиваю C# переменным содержание java переменных:
public class ReceiveNotificationsPlugin : IDisposable
{
private const string CLASS_NAME = "com.example.getnotificationslibrary.ReceiveNotificationsPlugin";
private AndroidJavaObject plugin;
private IReceiveNotificationsCallback callback;
public TextMeshPro TextToShowTitle;
public TextMeshPro TextToShowBody;
private static AndroidJavaObject javaClass = new AndroidJavaClass(CLASS_NAME);
public string From;
public string body;
public void Start()
{
plugin.Call("onNotificationPosted", callback);
}
public ReceiveNotificationsPlugin()
{
plugin = new AndroidJavaObject("com.example.getnotificationslibrary.ReceiveNotificationsPlugin");
callback = new IReceiveNotificationsCallback();
// callback.launchCameraApp += Callback_launchCameraApp;
}
public void Assign()
{
From = javaClass.Get<string>("title");
body = javaClass.Get<string>("text");
}
public void Dispose()
{
if (plugin != null)
{
plugin.Dispose();
}
plugin = null;
}
}
вот тут я вызываю C# функцию которая вызывает java функцию и присваиваю TextMeshPro объекту содержимое переменных, которым я ранее присвоил содержимое java переменных:
public class AssignJavaVariablesWithUnity : MonoBehaviour
{
private ReadSMSOn readSMSOn;
private ReceiveNotificationsPlugin plugin;
public TextMeshPro Title;
public TextMeshPro Body;
private void Start()
{
plugin = new ReceiveNotificationsPlugin();
readSMSOn = FindObjectOfType<ReadSMSOn>();
}
void Update()
{
if (readSMSOn.ButtonToStartReadSMS)
{
plugin.Start();
}
Title.text = plugin.From;
Body.text = plugin.body;
plugin.Assign();
}
}
может сам java код не верный? или может нужно что-то также изменить в AndroidManifest? так как когда я открыл мой апк файл через андроид студио, и там открыл AndroidManifest - то никаких следов моих плагинов не увидел... правильно ли я работаю с плагином с помощью C# кода? заранее спасибо!