【发布时间】:2020-05-14 18:36:14
【问题描述】:
我在 FCM 上设置了一个应用程序,以在 Xamarin.Forms 中向 android 设备发送通知,但只收到通知,但系统托盘中不显示图像。
这是我的 AndroidManifest.xml
<application android:label="DMSMobileApp.Android">
<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>
</application>
我创建了 FirebaseMessagingService.cs 来接收并转换为本地通知。
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
var body = message.GetNotification().Body;
Log.Debug(TAG, "Notification Message Body: " + body);
SendNotification(body, message.Data);
//new NotificationHelper().CreateNotification(message.GetNotification().Title, message.GetNotification().Body);
}
void SendNotification(string messageBody, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}
var pendingIntent = PendingIntent.GetActivity(this,
MainActivity.NOTIFICATION_ID,
intent,
PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.notification_template_icon_bg)
.SetContentTitle("FCM Message")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
}
}
我通过 REST API 发送通知。
var data = new
{
to = "/topics/ALL", // Recipient device token
notification = new {
title = "Test",
body = "Message",
image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
},
image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
};
var jsonBody = JsonConvert.SerializeObject(data);
bool fcmState;
using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send"))
{
httpRequest.Headers.TryAddWithoutValidation("Authorization", serverKey);
httpRequest.Headers.TryAddWithoutValidation("Sender", senderId);
httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
var result = await httpClient.SendAsync(httpRequest);
if (result.IsSuccessStatusCode)
{
fcmState = true;
}
else
{
// Use result.StatusCode to handle failure
// Your custom error handler here
//_logger.LogError($"Error sending notification. Status Code: {result.StatusCode}");
}
}
}
- 我可以在后台接收通知,但在前台,我只能收到声音,但系统托盘中没有通知。
- 当应用在后台运行但未收到图像时,我在系统托盘中收到通知。
【问题讨论】:
标签: xamarin xamarin.forms