Xamarin Forms Android在关闭时未收到推送通知 [英] Xamarin Forms Android not receiving Push notifications when closed

查看:118
本文介绍了Xamarin Forms Android在关闭时未收到推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照此演练在Xamarin表单应用中创建推送通知

I am following this walkthrough for creating push notifications into a Xamarin forms app

我已成功与NotificationHub集成,并且当应用程序处于焦点或后台时可以在设备上接收测试消息

I have successfully integrated with NotificationHub and can receive test messages on the device when app is in focus or in background

但是,我无法设法唤醒应用程序以接收消息,这是收到通知的主要原因

I cannot, however, manage to make the app wake in order to receive messages, which is the main reason for having notifications

关于此问题的帖子很多,我花了整个下午阅读了很多帖子,但是即使其他人坚持要这样做,我仍未找到任何有效的方法.

There are a great many posts about this issue and I have spent the afternoon reading a lot of them, but I have yet to find anything that works, even when others insist they do.

我是在Motorola设备上构建的,而不是在模拟器上构建的

I am building on a Motorola device, not an emulator

我的清单部分看起来像这样:

My manifest looks, in part, like this:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="${applicationId}" />
  </intent-filter>
</receiver>

SendNotification方法-我修改了示例代码,使其能够在Android UI中显示

The SendNotification method - I modified the example code to enable it to display in the Android UI, which it does

        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        intent.PutExtra("message", body);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new NotificationCompat.Builder(this, Authenticate.Constants.NotificationChannelName)
            .SetContentTitle("XamarinNotify Message")
            .SetSmallIcon(Resource.Drawable.ic_launcher)
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetShowWhen(false)
            .SetContentIntent(pendingIntent);

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            notificationBuilder.SetChannelId(Authenticate.Constants.NotificationChannelName);
        }

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());

        // Awesome - this displays the message in the UI but only when the app is running

        var notificationManagerCompat = NotificationManagerCompat.From(this);
        notificationManager.Notify(0, notificationBuilder.Build());

但是,如果我强制停止应用程序,则通知将永远不会到来,除非手动重新启动应用程序

However, if I force stop the app, the notification will never arrive until the app is restarted manually

我不知道:

  1. 我做错了什么吗?
  2. 如果我需要实施某种服务,那是什么?会唤醒应用程序的东西吗?我曾考虑过让一个带有PeriodicWorkRequest的Worker,但无法确定这是否是正确的方法,如果可以的话,我希望在计时器运行时调用它吗?
  3. 我也研究了BroadcastReceiver,但是Microsoft网站上的示例中使用的某些软件包现在已被弃用,例如WakefulBroadcastReceiver,这使我不得不调查2?
  1. Have I done something wrong?
  2. If I need to implement a service of some kind, what is it? Something that would wake the app? I looked into having a Worker with a PeriodicWorkRequest but then couldn't determine if this was the right approach and if so what am I looking to have it call when the timer runs?
  3. I also looked into BroadcastReceiver but some of the packages used in the example on the Microsoft site have now been deprecated, like WakefulBroadcastReceiver, which lead me to investigate 2?

推荐答案

尝试在对Firebase API的请求中放入JSON密钥"notification",但改用"data"

try to put JSON key 'notification' in your request to firebase API but instead use 'data'

{
 "to": "/topics/journal",
 "notification": {
 "title" : "title",
 "text": "data!",
 "icon": "ic_notification"
  }
}

在这种情况下,仅当您的应用位于前景

in this case,these messages trigger the onMessageReceived() callback only when your app is in foreground

{
 "to": "/topics/dev_journal",
 "data": {
   "text":"text",
   "title":"",
   "line1":"Journal",
   "line2":"刊物"
  }
}   

在这种情况下,即使您的应用位于前景/背景/被杀死

in this case,theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed

但这是一个主题,它也取决于设备,并且FCM无法完全控制.

but this is a topic that depends also on the device and that FCM has no total control over.

您可以参考的更多信息 https://stackoverflow.com/a/37845174/10768653

the more you could refer to https://stackoverflow.com/a/37845174/10768653

https://stackoverflow.com/a/43131488/10768653

这篇关于Xamarin Forms Android在关闭时未收到推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆