【发布时间】:2019-06-18 00:35:24
【问题描述】:
我使用 Azure Notification Hub 和 Firebase 为 Xamarin.Android 应用配置了推送通知。我关注了这个tutorial
当从 Azure 通知中心 发送测试推送通知时,我可以看到我的通知代码被调用,所以看起来一切都已配置。没有错误发生,但也没有收到推送通知。
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
Log.Debug(TAG, "From: " + message.From);
if (message.GetNotification() != null)
{
//These is how most messages will be received
Log.Debug(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 NotificationCompat.Builder(this)
.SetContentTitle("FCM Message")
.SetSmallIcon(Resource.Drawable.drivingalert)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build()); // this should send the notification!!
}
}
我可以单步执行代码,一切似乎都可以正常工作,但没有收到推送通知。
【问题讨论】:
-
您在哪个版本的 Android 上进行测试?如果您在使用奥利奥,您还需要创建一个通知渠道。
-
所以调用了发送通知方法?
-
@G.hakim 是的,通知方法被调用,没有遇到错误。但是没有收到通知。
-
@Cheesebaron 如何设置通知渠道?
-
@DomBurf 我已经在下面添加了通知通道代码
标签: xamarin xamarin.forms xamarin.android azure-notificationhub