Android Xamarin - 重启后接收通知会导致应用程序服务崩溃

时间:2016-11-29 10:12:04

标签: android xamarin service notifications broadcastreceiver

我已在Xamarin for Android中创建了一个应用程序,该应用程序通过GCM接收通知。除非用户重新启动设备,否则通知正常。如果设备在打开应用程序之前收到通知,则通知服务将崩溃并且用户将得到一个"应用程序已停止工作"弹出。如果应用程序在重新启动后打开一次,则可以再次正常工作。

我一直试图弄清楚过去2天导致此次崩溃的原因,但我尝试的任何事情似乎都无法解决问题。有谁知道造成这个问题的原因是什么?

广播接收器

[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.INTERNET")]
[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]
namespace XamarinAlerting.Droid.Services.Notifications {
    [BroadcastReceiver(Permission = Constants.PERMISSION_GCM_INTENTS)]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    [IntentFilter(new string[] { Constants.INTENT_FROM_GCM_MESSAGE },
     Categories = new string[] { "@PACKAGE_NAME@" })]
    [IntentFilter(new string[] { Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK },
     Categories = new string[] { "@PACKAGE_NAME@" })]
    [IntentFilter(new string[] { Constants.INTENT_FROM_GCM_LIBRARY_RETRY },
     Categories = new string[] { "@PACKAGE_NAME@" })]
    class AzureNotificationReceiver : GcmBroadcastReceiverBase<AzureNotificationService> { 
    }
}

GCM服务

namespace XamarinAlerting.Droid.Services.Notifications {
    [Service(Exported = false)]
    class AzureNotificationService : GcmServiceBase {

        public AzureNotificationService() : base(AzureNotificationConfiguration.SenderId) { }


        protected override void OnError(Context context, string errorId) {
            Toast.MakeText(this, "OnError", ToastLength.Long).Show();
        }

        protected override void OnMessage(Context context, Intent intent) {
            string title = intent.Extras.GetString("title");
            string text = intent.Extras.GetString("message");
            string image = intent.Extras.GetString("image");
            string sound = intent.Extras.GetString("sound");

            var builder = new Notification.Builder(Forms.Context)
                .SetContentTitle(title)
                .SetContentText(text)
                .SetSmallIcon(Resource.Drawable.test24px);
            var notification = builder.Build();
            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(0, notification);
        }

        protected override void OnRegistered(Context context, string registrationId) {
            Settings.RegistrationId = registrationId;

            var hub = new NotificationHub(AzureNotificationConfiguration.HubName, AzureNotificationConfiguration.ConnectionString, context);
            try {
                hub.UnregisterAll(registrationId);
            } catch (Exception ex) {
                Toast.MakeText(this, "Unregistration Exception", ToastLength.Long).Show();
            }

            var tags = new List<string>() { };
            try {
                var hubRegistration = hub.Register(registrationId, tags.ToArray());
            } catch (Exception ex) {
                Toast.MakeText(this, "Registratin Exception", ToastLength.Long).Show();
            }
        }

        protected override void OnUnRegistered(Context context, string registrationId) {
            Toast.MakeText(this, "OnUnRegister", ToastLength.Long).Show();
        }
    }
}

0 个答案:

没有答案