在 Xamarin 中使用 Azure 推送通知时始终获取 JsonReaderException Android
Always get JsonReaderException while using Azure Push Notification in Xamarin Android
我正在尝试使用 azure 推送通知示例,就像在这个示例中一样 https://github.com/xamarin/customer-success-samples/tree/master/samples/Xamarin.Android/AzurePushNotification.Android
我完成了所有步骤,我可以注册设备,获取注册 ID 没有问题,但是当我尝试从 Azure 门户发送测试推送消息时,我总是收到此 JsonReaderException 错误。感谢任何帮助,谢谢。
默认测试消息=
{"data":{"message":"Notification Hub test notification"}}
错误是
"Newtonsoft.Json.JsonReaderException: Error reading JObject from
JsonReader. Current JsonReader item is not an object: Integer. Path
''"
和 GcmServiceBase(与示例完全一样)
namespace myapp.Droid
{
[BroadcastReceiver(Permission = Constants.PERMISSION_GCM_INTENTS)]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_MESSAGE },
Categories = new string[] { "myapp.Droid" })]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK },
Categories = new string[] { "myapp.Droid" })]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_LIBRARY_RETRY },
Categories = new string[] { "myapp.Droid" })]
public class GcmBroadcastReceiver : GcmBroadcastReceiverBase<GcmService>
{
public static string[] SENDER_IDS = { "my firebase sender id" };
public const string HUB_NAME = "my hub namespace ";
public const string HUB_LISTEN_SECRET = "SharedAccessKey in the azure";
}
[Service]
public class GcmService : GcmServiceBase
{
private static NotificationHub hub;
public GcmService() : base(GcmBroadcastReceiver.SENDER_IDS) { }
public static void Initialize(Context context)
{
var cs = ConnectionString.CreateUsingSharedAccessKeyWithListenAccess(
new Java.Net.URI ("sb://" + GcmBroadcastReceiver.HUB_NAME(actually I write my hub namespace in here bcz when I write hub name ,it gives NotificationHubResourceNotFoundException error,so I write Hub Namespace) + "-ns.servicebus.windows.net/"),
GcmBroadcastReceiver.HUB_LISTEN_SECRET);
hub = new NotificationHub(GcmBroadcastReceiver.HUB_NAME, cs, context);
}
public static void Register(Context Context)
{
GcmClient.Register(Context, GcmBroadcastReceiver.SENDER_IDS);
}
protected override void OnRegistered(Context context, string registrationId)
{
//Receive registration Id for sending GCM Push Notifications to
if (hub != null)
{
var registration = hub.Register(registrationId, "TEST");
}
}
protected override void OnUnRegistered(Context context, string registrationId)
{
if (hub != null)
{
hub.Unregister();
}
}
protected override void OnMessage(Context context, Intent intent)
{
// Push Notification arrived
ShowLocalNotification(intent);
// Do additiona messaging here...
}
private void ShowLocalNotification(Intent intent)
{
//Push Notification arrived
if (intent != null || intent.Extras != null)
{
var msg = intent.Extras.GetString("message");
msg = String.IsNullOrEmpty(msg) ? "No Message" : msg;
// Instantiate the builder and set notification elements:
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("Push Notification Received")
.SetContentText(msg)
.SetDefaults(NotificationDefaults.Sound);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
}
}
protected override bool OnRecoverableError(Context context, string errorId)
{
//Some recoverable error happened
return true;
}
protected override void OnError(Context context, string errorId)
{
//Some more serious error happened
}
}
}
而mainactivity是这样的
[assembly: Permission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "@PACKAGE_NAME@.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")]
namespace AzurePushNotification.Android
{
[Activity (Label = "myapp.Android", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Initialize our Gcm Service Hub
GcmService.Initialize (this);
// Register for GCM
GcmService.Register (this);
}
}
}
我终于找到了错误,https://www.nuget.org/packages/Xam.Plugin.AzurePushNotification/这个插件导致了这个错误。当我卸载这个时,不再error.I猜测这个azurepush插件必须单独使用,它有点覆盖了一些方法,如果你尝试直接接收推送消息 GCM.So 仅使用此插件 alone.Dont 如果您不使用此插件,请将其保留在项目中。
我正在尝试使用 azure 推送通知示例,就像在这个示例中一样 https://github.com/xamarin/customer-success-samples/tree/master/samples/Xamarin.Android/AzurePushNotification.Android
我完成了所有步骤,我可以注册设备,获取注册 ID 没有问题,但是当我尝试从 Azure 门户发送测试推送消息时,我总是收到此 JsonReaderException 错误。感谢任何帮助,谢谢。 默认测试消息=
{"data":{"message":"Notification Hub test notification"}}
错误是
"Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: Integer. Path ''"
和 GcmServiceBase(与示例完全一样)
namespace myapp.Droid
{
[BroadcastReceiver(Permission = Constants.PERMISSION_GCM_INTENTS)]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_MESSAGE },
Categories = new string[] { "myapp.Droid" })]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK },
Categories = new string[] { "myapp.Droid" })]
[IntentFilter(new string[] { Constants.INTENT_FROM_GCM_LIBRARY_RETRY },
Categories = new string[] { "myapp.Droid" })]
public class GcmBroadcastReceiver : GcmBroadcastReceiverBase<GcmService>
{
public static string[] SENDER_IDS = { "my firebase sender id" };
public const string HUB_NAME = "my hub namespace ";
public const string HUB_LISTEN_SECRET = "SharedAccessKey in the azure";
}
[Service]
public class GcmService : GcmServiceBase
{
private static NotificationHub hub;
public GcmService() : base(GcmBroadcastReceiver.SENDER_IDS) { }
public static void Initialize(Context context)
{
var cs = ConnectionString.CreateUsingSharedAccessKeyWithListenAccess(
new Java.Net.URI ("sb://" + GcmBroadcastReceiver.HUB_NAME(actually I write my hub namespace in here bcz when I write hub name ,it gives NotificationHubResourceNotFoundException error,so I write Hub Namespace) + "-ns.servicebus.windows.net/"),
GcmBroadcastReceiver.HUB_LISTEN_SECRET);
hub = new NotificationHub(GcmBroadcastReceiver.HUB_NAME, cs, context);
}
public static void Register(Context Context)
{
GcmClient.Register(Context, GcmBroadcastReceiver.SENDER_IDS);
}
protected override void OnRegistered(Context context, string registrationId)
{
//Receive registration Id for sending GCM Push Notifications to
if (hub != null)
{
var registration = hub.Register(registrationId, "TEST");
}
}
protected override void OnUnRegistered(Context context, string registrationId)
{
if (hub != null)
{
hub.Unregister();
}
}
protected override void OnMessage(Context context, Intent intent)
{
// Push Notification arrived
ShowLocalNotification(intent);
// Do additiona messaging here...
}
private void ShowLocalNotification(Intent intent)
{
//Push Notification arrived
if (intent != null || intent.Extras != null)
{
var msg = intent.Extras.GetString("message");
msg = String.IsNullOrEmpty(msg) ? "No Message" : msg;
// Instantiate the builder and set notification elements:
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("Push Notification Received")
.SetContentText(msg)
.SetDefaults(NotificationDefaults.Sound);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
}
}
protected override bool OnRecoverableError(Context context, string errorId)
{
//Some recoverable error happened
return true;
}
protected override void OnError(Context context, string errorId)
{
//Some more serious error happened
}
}
}
而mainactivity是这样的
[assembly: Permission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "@PACKAGE_NAME@.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")]
namespace AzurePushNotification.Android
{
[Activity (Label = "myapp.Android", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Initialize our Gcm Service Hub
GcmService.Initialize (this);
// Register for GCM
GcmService.Register (this);
}
}
}
我终于找到了错误,https://www.nuget.org/packages/Xam.Plugin.AzurePushNotification/这个插件导致了这个错误。当我卸载这个时,不再error.I猜测这个azurepush插件必须单独使用,它有点覆盖了一些方法,如果你尝试直接接收推送消息 GCM.So 仅使用此插件 alone.Dont 如果您不使用此插件,请将其保留在项目中。