Xamarin Forms Android 在关闭时未收到推送通知 [英] Xamarin Forms Android not receiving Push notifications when closed
问题描述
我正在按照此演练将推送通知创建到 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.
我在摩托罗拉设备上构建,而不是模拟器
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
我不知道:
- 我做错了什么吗?
- 如果我需要实现某种服务,那是什么?会唤醒应用程序的东西?我研究了一个带有 PeriodicWorkRequest 的 Worker,但随后无法确定这是否是正确的方法,如果是,我希望在计时器运行时调用它什么?
- 我还研究了 BroadcastReceiver,但 Microsoft 网站上的示例中使用的一些包现在已被弃用,例如 WakefulBroadcastReceiver,这让我开始调查 2?
推荐答案
尝试将 JSON 键 'notification' 放在您对 firebase API 的请求中,而是使用 '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
和https://stackoverflow.com/a/43131488/10768653
这篇关于Xamarin Forms Android 在关闭时未收到推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!