Xamarin Android通知仅工作一次

时间:2019-01-14 14:58:44

标签: android firebase xamarin push-notification

总而言之,我正在使用Firebase Messaging Service,并且正在尝试让我的消息在应用程序关闭/关闭时显示。奇怪的问题是这种作品。如果我关闭该应用程序并在Azure Messaging Portal中按send,我将收到一条通知,这是我收到的唯一通知,直到我打开该应用程序并再次关闭它,然后我才能再收到一个通知。如果该应用已打开,我可以发送垃圾邮件,并且我将收到所有通知。

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
[IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, 
Categories = new string[] { "PACKAGEIDENTIFIER" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirstBaseMessagingService";
private int Notification_ID = 2;
public override void OnMessageReceived(RemoteMessage message)
{
    Log.Debug(TAG, "From: " + message.From);
    if (message.GetNotification() != null)
    {
        Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
        SendNotification(message.Data);
    }
    else
    {
        SendNotification(message.Data);

    }
}

void SendNotification(IDictionary<string,string> message)
{
    Drawable drawable = ContextCompat.GetDrawable(this, Resource.Drawable.lsbu);
    Bitmap bitmap = ((BitmapDrawable)drawable).Bitmap;

    string title = message["title"];
    string body = message["body"];

    NotificationManager notificationManager = GetSystemService(NotificationService) as NotificationManager;
    string ChannelID =  GetString(Resource.String.LocalChannelID);
    string AppName = GetString(Resource.String.AppName);
    NotificationImportance Importance = NotificationImportance.Default;
    NotificationChannel mChannel = new NotificationChannel(ChannelID, AppName, Importance);
    notificationManager.CreateNotificationChannel(mChannel);

    Notification.Builder builder = new Notification.Builder(this, ChannelID)
            .SetWhen(DateTime.Now.Second)
            .SetSmallIcon(Resource.Drawable.miniwozzad)
            .SetLargeIcon(bitmap)
            .SetContentTitle(title)
            .SetContentText(body)
            .SetChannelId(ChannelID);

    var notification = builder.Build();
    notificationManager.Notify(Notification_ID, notification);
    Notification_ID++;
}

}

这是我在Azure测试门户中的有效负载

{"data":{"body": "Another big message", "test": 1, "title": "A Big Title2" }}

这是我的FirebaseMessagingService类,我不确定这里缺少什么。任何帮助将不胜感激!

0 个答案:

没有答案