【发布时间】:2019-07-01 04:55:39
【问题描述】:
我之前已经启动了推送通知并且可以正常工作,但现在由于某种原因虽然收到了推送通知,(我知道它已收到,因为它在 Debug 中显示已收到 FireBaseMessage),它不再显示或通知没有不出现。
以下是我的 MyFireBaseMessagingService.cs 中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using System.Diagnostics;
using Firebase.Messaging;
namespace Chatfer.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
System.Diagnostics.Debug.WriteLine(TAG, "From: " + message.From);
if (message.GetNotification() != null)
{
//These is how most messages will be received
System.Diagnostics.Debug.WriteLine(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(message.GetNotification().Body);
}
else
{
//Only used for debugging payloads sent from the Azure portal
SendNotification(message.Data.Values.First());
}
}
void SendNotification(string messageBody)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Notification.Builder(this)
.SetContentTitle("Chatfer Message")
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentText(messageBody)
.SetAutoCancel(false)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
}
发生了什么事?为什么通知突然停止出现?任何见解或建议将不胜感激!
编辑:将 MyFirebaseMessagingService.cs 更改为以下内容后,通知再次开始显示!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using System.Diagnostics;
using Firebase.Messaging;
namespace Chatfer.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
System.Diagnostics.Debug.WriteLine(TAG, "From: " + message.From);
if (message.GetNotification() != null)
{
//These is how most messages will be received
System.Diagnostics.Debug.WriteLine(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(message.GetNotification().Body);
}
else
{
//Only used for debugging payloads sent from the Azure portal
SendNotification(message.Data.Values.First());
}
}
void SendNotification(string messageBody)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Notification.Builder(this)
.SetContentTitle("Chatfer Message")
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetChannelId("YourUniqueChannelID");
//var notificationManager = NotificationManager.FromContext(this);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel("YourUniqueChannelID", "FCM Notifications", NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
}
【问题讨论】:
-
问题是我已将我的 TargetAPIVersion 更新为 26。请参考forums.xamarin.com/discussion/106314/…?了解更多信息
-
Bhai,我希望我能再投票十几次。您的修改帮助我解决了我几天来遇到的问题。
-
哈哈哈没问题,一切顺利!
标签: firebase xamarin xamarin.forms push-notification firebase-cloud-messaging