I am using xam.plugin.pushnotification in my xamarin.forms project
my main activity
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>("mysendernumbers");
StartPushService();
CrossPushNotification.Current.Register();
}
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);
}
}
My listener in my pcl
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;
}
}
}
Registering (trying LOL) in app.cs (PCL)
public App()
{
InitializeComponent();
CrossPushNotification.Current.Register();
MainPage = new NavigationPage(new Views.Splash2());
}
I am getting this error after try to register....(in onerror method)
Java.IO.IOException: INVALID_PARAMETERS\\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 \\n at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <25999cc1fde6426b852c2e027711c254>:0 \\n at Android.Runtime.JNIEnv.CallVoidMethod (System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in <7a2bbe1fcaec4467930361ae212aeea9>:0 \\n at Android.Gms.Gcm.GcmPubSub.Subscribe (System.String registrationToken, System.String topic, Android.OS.Bundle extras) [0x00096] in <442f26a0595046369060be9502394e67>:0 \\n at PushNotification.Plugin.PushNotificationRegistrationIntentService.SubscribeTopics (System.String token) [0x00023] in <06cf100f14f94017b47f90010ef822f3>:0 \\n at PushNotification.Plugin.PushNotificationRegistrationIntentService.OnHandleIntent (Android.Content.In tent intent) [0x0004a] in <06cf100f14f94017b47f90010ef822f3>:0 \\n --- End of managed Java.IO.IOException stack trace ---\\njava.io.IOException: INVALID_PARAMETERS\\n\\tat com.google.android.gms.iid.zzc.zzb(Unknown Source)\\n\\tat com.google.android.gms.iid.zzc.zza(Unknown Source)\\n\\tat com.google.android.gms.iid.InstanceID.zzc(Unknown Source)\\n\\tat com.google.android.gms.iid.InstanceID.getToken(Unknown Source)\\n\\tat com.google.android.gms.gcm.GcmPubSub.subscribe(Unknown Source)\\n\\tat md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.n_onHandleIntent(Native Method)\\n\\tat md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.onHandleIntent(PushNotificationRegistrationIntentService.java:37)\\n\\tat android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)\\n\\tat android.os.Handler.dispatchMessage(Handler.java:102)\\n\\tat android.os.Looper.loop(Looper.java:146)\\n\\tat android.os.HandlerThread.run(HandlerThread.java:61)\\n - Regis ter - PushNotificationRegistationIntentService
I guess its not the exact answer you've been looking for, but this package you, me and many others are using, was recently announced as DEPRECATED . If you ain't public with PushNotifications yet you should strongly consider migration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.