应用关闭时,xamarin android FCM链接不起作用

时间:2018-07-06 11:27:11

标签: c# xamarin.android

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Android.App;
    using Android.Content;
    using Android.Media;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Firebase.Messaging;
    using SSDMobileApp.Activities;
    using Firebase.Iid;
    using Android.Graphics;
    using System.IO;

    namespace SSDMobileApp
    {
    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    public class MyFirebaseIIDService : FirebaseInstanceIdService
    {
    const string TAG = "MyFirebaseIIDService";
    public override void OnTokenRefresh()
    {
    var refreshedToken = FirebaseInstanceId.Instance.Token;
    //Log.Debug(TAG, "Refreshed token: " + refreshedToken);
    SendRegistrationToServer(refreshedToken);
    }
    void SendRegistrationToServer(string token)
    {
    // Add custom implementation, as needed.
    }
    }

    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    class FCMSendMessage : FirebaseMessagingService
    {

    public override void OnMessageReceived(RemoteMessage message)
    {
    base.OnMessageReceived(message);
    SendNotification(message.GetNotification().Body, message.GetNotification().Title, message);
    }

    private void SendNotification(string Body, string Title, RemoteMessage message)
    {
    string value = "";
    string key = "";
    Bitmap imageBitmap = CommonMethods.GetImageBitmapFromUrl( CommonMethods.IpAddress +  "/Content/images/logo.png");
    if (message.Data != null)
    {
    if (message.Data.Any())
    {
    foreach (KeyValuePair<string, string> kv in message.Data)
    {
    if (!string.IsNullOrEmpty(kv.Key) && !string.IsNullOrEmpty(kv.Value))
    {
    key = kv.Key;
    value = kv.Value;
    break;
    }
    }
    }
    }

    if (value != "")
    {
    var uri = Android.Net.Uri.Parse(value);
    var intent = new Intent(Intent.ActionView, uri);
    intent.AddFlags(ActivityFlags.ClearTop);
    var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
    var defaultsounduri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
    var notificationBuilder = new Notification.Builder(this)
    .SetSmallIcon(Resource.Drawable.megaphone)
    .SetContentTitle(Title)
    .SetContentText(Body)
    .SetAutoCancel(true)
    .SetContentIntent(pendingIntent)
    .SetLargeIcon(imageBitmap);
    var notificationManager = NotificationManager.FromContext(this);
    notificationManager.Notify(0, notificationBuilder.Build());
    }
    else
    {
    var intent = new Intent(this, typeof(HomePageActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
    var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
    var defaultsounduri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
    var notificationBuilder = new Notification.Builder(this)
    .SetSmallIcon(Resource.Drawable.megaphone)
    .SetContentTitle(Title)
    .SetContentText(Body)
    .SetAutoCancel(true)
    .SetContentIntent(pendingIntent)
    .SetLargeIcon(imageBitmap);
    var notificationManager = NotificationManager.FromContext(this);
    notificationManager.Notify(0, notificationBuilder.Build());
    }

    }
    }
    }

关闭应用程序时,Xamarin android FCM无法正常工作。因此,请提供一些帮助。在此先感谢。我已经尝试了一切,但仍然无法正常工作。以前它可以正常工作。关闭应用程序时,Xamarin android FCM无法正常工作。因此,请提供一些帮助。在此先感谢。我已经尝试了一切,但仍然无法正常工作。以前它正常工作。

0 个答案:

没有答案