Добавить данные из Android-уведомления в Android-приложение
Я использую Xamarin - Visual Studio 15 для Android.
Я отправляю Уведомления в свое приложение, используя служебную шину Azure и Google Cloud Messaging.
Все отлично работает Теперь мне бы хотелось, чтобы - каждый раз, когда поступало уведомление - если приложение работает (или работает в фоновом режиме), - приложение будет собирать информацию из уведомления.
Структура приложения:
Класс деятельности:
using ...
namespace qtosof
{
[Activity (MainLauncher = true, Icon="@drawable/ic_launcher", Label="@string/app_name", Theme="@style/AppTheme")]
public class ToDoActivity : Activity
{
static ToDoActivity instance = new ToDoActivity();
public static ToDoActivity CurrentActivity
{
get { return instance; }
}
#endregion
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Activity_To_Do);
instance = this;
Button setEmail = FindViewById<Button>(Resource.Id.setButt);
EditText editMail = FindViewById<EditText>(Resource.Id.editMail);
setEmail.Click += delegate
{
string temp = editMail.Text;
GcmConnect(temp);
setEmail.Enabled = false;
};
}
public void GcmConnect(string uniqeMail)
{
string getUniqeEmailUser = uniqeMail;
string getAndroidId = Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
userData.SetUniqeData(getUniqeEmailUser, getAndroidId);
try
{
GcmClient.CheckDevice(this);
GcmClient.CheckManifest(this);
GcmClient.Register(this, ToDoBroadcastReceiver.senderIDs);
}
catch
{
System.Diagnostics.Debug.WriteLine("The device has Problem to Register GcmClient.", "PROBLEM!");
}
}
}
}
using...
[assembly: Permission(Name = "XXXXXX.XXXXXX.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "XXXXXX.XXXXXX.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]
[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]
[assembly: UsesPermission(Name = "android.permission.INTERNET")]
[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]
[BroadcastReceiver(Permission = Gcm.Client.Constants.PERMISSION_GCM_INTENTS)]
[IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_MESSAGE }, Categories = new string[] { "XXXXXX.XXXXXX" })]
[IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new string[] { "XXXXXX.XXXXXX" })]
[IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new string[] { "XXXXXX.XXXXXX" })]
public class ToDoBroadcastReceiver : GcmBroadcastReceiverBase<PushHandlerService>
{
public static string[] senderIDs = new string[] { "XXXXXXXXXXXX" };
}
[Service]
public class PushHandlerService : GcmServiceBase
{
public static int numOfQue = 1;
private NotificationHub Hub { get; set; }
public PushHandlerService() : base(ToDoBroadcastReceiver.senderIDs){}
protected override void OnRegistered(Context context, string registrationId)
{
System.Diagnostics.Debug.WriteLine("The device has been registered with GCM.", "Success!");
string connectionString = "Endpoint=sb://XXXXXXXXXXXX.XXXXXXXXXXXX.XXXXXXXXXXXX.XXXXXX/;SharedAccessKeyName=XXXXXXXXXXXX;SharedAccessKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
string notificationHubPath = "XXXXXXXXXXXXXXXXXX";
Hub = new NotificationHub(notificationHubPath, connectionString, context);
try
{
Hub.UnregisterAll(registrationId);
}
catch
{
System.Diagnostics.Debug.WriteLine("The device has Problem to UnregisterAll.", "PROBLEM!");
}
string EmailTag = userData.GetUniqeData(); // get the uniqe Id from User login data, For example - let's say that the user e-mail is unique
string AndroidTag = userData.GetUniqeData("AndroidId");
var tags = new List<string>() { EmailTag, AndroidTag };
try
{
var hubRegistration = Hub.Register(registrationId, tags.ToArray());
}
catch
{
System.Diagnostics.Debug.WriteLine("The device has Problem to Register.", "PROBLEM!");
}
}
protected override void OnMessage(Context context, Intent intent)
{
//TODO... all is work fine
}
protected override void OnUnRegistered(Context context, string registrationId)
{
throw new NotImplementedException();
}
protected override void OnError(Context context, string errorId)
{
System.Diagnostics.Debug.WriteLine(
string.Format("Error occurred in the notification: {0}.", errorId));
}
}
Я думал использовать статическую переменную, но я не уверен, что это правильный путь...
Спасибо за ваше время