当Android应用程序“关闭”时,无法收到推送通知

4

我正在使用GCM服务进行推送通知,当应用程序处于打开或后台状态时,它可以正常工作,但是当应用程序关闭时,它会崩溃:

enter image description here

异常信息:

FATAL EXCEPTION: main Process: br.com.siagri.SiagriAutorize, PID: 15786 android.runtime.JavaProxyThrowable: System.NotSupportedException: Could not activate JNI Handle 0xbeed8498 (key_handle 0xa7376b4) of Java type 'md5d8b390bb35897e87a51d1b4384dffa30/GcmService' as managed type 'SiagriAuth.Droid.Services.GcmService'. ---> System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it.

我按照这个 教程 进行操作,并查看了这个示例,但在应用程序关闭时没有发现任何关于/不同的通知工作方式。

这是我的接收和处理通知的方法:

 private void CreateNotification(string title, string desc, string approvalCode)
        {
            try
            {
                //Create notification
                var notificationManager = GetSystemService(NotificationService) as NotificationManager;

                //Create an intent to show ui
                var uiIntent = new Intent(this, typeof(MainActivity));

                //Use Notification Builder
                var builder = new NotificationCompat.Builder(this);

                //Create the notification
                //we use the pending intent, passing our ui intent over which will get called
                //when the notification is tapped.
                var notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, uiIntent, 0))
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetTicker(title)
                        .SetContentTitle(title)
                        .SetContentText(desc)
                        .SetPriority((int)NotificationPriority.High)
                        .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
                        //Set the notification sound
                        .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                        //Auto cancel will remove the notification once the user touches it
                        .SetAutoCancel(true).Build();
                //Show the notification

                int uniqueIdentifier = 0;
                for (int i = 0; i < approvalCode.Length; i++)
                {
                    uniqueIdentifier += (int)approvalCode[i];
                }
                notificationManager?.Notify(uniqueIdentifier, notification);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                App.LocalAlert("Erro", "Erro ao criar notificação.");
            }
        }

        protected override void OnMessage(Context context, Intent intent)
        {
            try
            {
                if (intent != null && intent.Extras != null)
                {
                    var messageText = intent.Extras.GetString("message");
                    var approvalCode = intent.Extras.GetString("ApprovalCode");
                    if (!string.IsNullOrEmpty(messageText) && !string.IsNullOrEmpty(approvalCode))
                        CreateNotification($"Solicitação - {approvalCode}", messageText, approvalCode);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                App.LocalAlert("Erro", "Erro ao criar notificação.");
            }
        }

我正在使用以下汇编语言:

[assembly: Permission(Name = "myProject.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "myProject.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")]
[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]

这些是IntentFilter:

 [BroadcastReceiver(Permission = Constants.PERMISSION_GCM_INTENTS)]
    [IntentFilter(new[] { Constants.INTENT_FROM_GCM_MESSAGE }, Categories = new[] { "br.com.siagri.SiagriAutorize" })]
    [IntentFilter(new[] { Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new[] { "br.com.siagri.SiagriAutorize" })]
    [IntentFilter(new[] { Constants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new[] { "br.com.siagri.SiagriAutorize" })]

有没有办法让这对于关闭的应用程序起作用?注意:我已经在发布模式下运行该应用程序。

我收到了一个错误信息。那么错误信息是什么? - SushiHangover
嗨@SushiHangover,抱歉说我收到了一个错误消息,应用程序崩溃了。我提供了一张图片。 - Pedro Franco
请查看logcat以获取崩溃的详细信息,它是在本地通知创建期间还是用户点击通知时崩溃的? - SushiHangover
我不知道它在哪里崩溃,因为它已经关闭了,所以我无法进行调试。当我在调试模式下运行时,背景和打开的操作都正常。@SushiHangover - Pedro Franco
1
Visual Studio->工具->Android->Android 设备监视器,选择你的手机,你会在控制台中看到日志。 - Robbit
显示剩余10条评论
1个回答

2

我的错误在于GcmService的构造中,我在那里实例化了我的repository,所以我试图访问数据库,我只是从构造函数中移除它并放到我的注册方法中,在那里我使用了它。

 public GcmService() : base(PushHandlerBroadcastReceiver.SenderIds)
    {
        Log.Info(PushHandlerBroadcastReceiver.Tag, "PushHandlerService() constructor");
        _permissaoRepository = new PermissaoRepository(App.SiagriAuthContext);
        _loginRepository = new LoginRepository(App.SiagriAuthContext);
    }

我改成了:

public GcmService() : base(PushHandlerBroadcastReceiver.SenderIds)
    {
        Log.Info(PushHandlerBroadcastReceiver.Tag, "PushHandlerService() constructor");
    }

当应用程序关闭时,我无法访问我的数据库,这是很合理的。我尝试了一下在应用程序关闭时使用数据库,但是没有找到相关信息。但是这里这里有一些关于使用本地数据库的内容。


节省了我好几天的时间。谢谢! - Björn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接