Почему я не могу зарегистрировать свое устройство Android? xam.pushnotification

Я использую xam.plugin.pushnotification в своем проекте xamarin.forms

моя основная деятельность

protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        //inicializa imageCircle
        ImageCircleRenderer.Init();

        //inicializa o mapa
        global::Xamarin.FormsMaps.Init(this, bundle);

        //shared Preferences
        App.Init(new AndroidUserPreferences());

        //Gerenciador de memória
        CachedImageRenderer.Init();

        try
        {
            AppContext = this.ApplicationContext;
            CrossPushNotification.Initialize<CrossPushNotificationListener>("my sender");
            StartPushService();
        }
        catch (Exception e)
        {
            var s = e.Message;
        }

        AndroidUserPreferences sharedPref = new AndroidUserPreferences();
        if ( sharedPref.GetString("token") == " ")
        {
            GetTokenTask myTask = new GetTokenTask();
            myTask.Execute(this);
        }

        LoadApplication(new App());
    }

    public static void StartPushService()
    {
        AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));

        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
        {

            PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
            alarm.Cancel(pintent);
        }
    }

    public static void StopPushService()
    {
        AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService)));
        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
        {
            PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
            alarm.Cancel(pintent);
        }
    }

Мой слушатель в моем ПК

public class  CrossPushNotificationListener : IPushNotificationListener
{

    public void OnMessage(JObject values, DeviceType deviceType)
    {
        Debug.WriteLine("Message Arrived");
    }

    public void OnRegistered(string token, DeviceType deviceType)
    {
        Debug.WriteLine(string.Format("Push Notification - Device Registered - Token : {0}", token));
    }

    public void OnUnregistered(DeviceType deviceType)
    {
        Debug.WriteLine("Push Notification - Device Unnregistered");

    }

    public void OnError(string message, DeviceType deviceType)
    {
        Debug.WriteLine(string.Format("Push notification error - {0}",message));
    }

    public bool ShouldShowNotification()
    {
        return true;
    }
}

}

Регистрация (пробуем LOL) в app.cs (PCL)

 public App()
    {
        InitializeComponent();

        CrossPushNotification.Current.Register();
        MainPage = new NavigationPage(new Views.Splash2());
    }

Я зарегистрировал свой проект в firebase, используя имя пакета, затем я создал там проект и получил идентификатор отправителя ... НО ... после вызова "cross ... current.register ()" где-то (он не отображается я где), у меня есть исключение

ФАТАЛЬНОЕ НЕОБРАБОТАННОЕ ИСКЛЮЧЕНИЕ: System.TypeLoadException: не удалось разрешить тип с токеном 0100005a (из typeref, класс / сборка Android.Gms.Gcm.Iid.InstanceID, Xamarin.GooglePlayServices.Gcm, Version = 1.0.0.0, Culture = нейтральный, PublicKeyToken = нулевой)

мне нужно установить xamarin.gcm в мой проект pcl? теперь это только в моем андроид-проекте


person Joyce de Lanna    schedule 15.09.2017    source источник
comment
Я использую Xam.Plugin.PushNotification-high. Пожалуйста, попробуйте этот nuget.   -  person Jorge Cruz    schedule 01.05.2019


Ответы (2)


Попробуйте вызвать CrossPushNotification.Current.Register (); в OnCreate метод. Нравится:

protected override void OnCreate(Bundle bundle)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(bundle);

    global::Xamarin.Forms.Forms.Init(this, bundle);

    //inicializa imageCircle
    ImageCircleRenderer.Init();

    //inicializa o mapa
    global::Xamarin.FormsMaps.Init(this, bundle);

    //shared Preferences
    App.Init(new AndroidUserPreferences());

    //Gerenciador de memória
    CachedImageRenderer.Init();

    try
    {
        AppContext = this.ApplicationContext;
        CrossPushNotification.Initialize<CrossPushNotificationListener>("my sender");
        //call register method here
        CrossPushNotification.Current.Register();
        StartPushService();
    }
    catch (Exception e)
    {
        var s = e.Message;
    }

    AndroidUserPreferences sharedPref = new AndroidUserPreferences();
    if ( sharedPref.GetString("token") == " ")
    {
        GetTokenTask myTask = new GetTokenTask();
        myTask.Execute(this);
    }

    LoadApplication(new App());
}
person Wilson Vargas    schedule 15.09.2017

Мне тоже пришлось перейти на бета-версию 1.2.5, она работает, НО плагин недавно был объявлен как УСТАРЕЛО.
Это плохие новости.

Для поддержки monoandroid80 мне пришлось разветвить xam.plugin.pushnotification и вручную обновить его packages.config.

Вскоре не останется другого выбора, кроме как мигрировать.

person Eyal.K    schedule 18.10.2017