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 密钥通知"放在对 Firebase API 的请求中,但使用数据"
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":"刊物"
}
}
在这种情况下,这些消息会触发 onMessageReceived()
回调,即使您的应用处于 foreground/background/killed
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屋!