Xamarin Forms: загрузка страницы содержимого при нажатии push-уведомлений Android - PullRequest
0 голосов
/ 28 марта 2019

Я получил тестовое уведомление с консоли FCM.Сейчас я пытаюсь открыть страницу при нажатии на уведомление.Есть идеи о том, как этого добиться?Я искал в Интернете, но не могу найти рабочее решение.Я также могу отправить уведомление через почтальона.

Ответы [ 2 ]

0 голосов
/ 29 апреля 2019

Я обрабатываю уведомление, нажав следующим образом.Загрузка страницы обрабатывается в App.xaml.cs.

On OnCreate ():

//Background or killed mode
if (Intent.Extras != null)
{
    foreach (var key in Intent.Extras.KeySet())
    {
        var value = Intent.Extras.GetString(key);
        if (key == "webContentList") 
        {
            if (value?.Length > 0)
            {
                isNotification = true;
                LoadApplication(new App(domainname, value));
            }
        }
    }
}
//Foreground mode
if (FirebaseNotificationService.webContentList.ToString() != "")
{
    isNotification = true;
    LoadApplication(new App(domainname, FirebaseNotificationService.webContentList.ToString()));
    FirebaseNotificationService.webContentList = "";
}

//Normal loading
if (!isNotification)
{
    LoadApplication(new App(domainname, string.Empty));
}

On FirebaseNotificationService:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseNotificationService : FirebaseMessagingService
{
    public static string webContentList = "";
    public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);
        webContentList = message.Data["webContentList"];

        try
        {
            SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:>>" + ex);
        }
    }

    public void SendNotificatios(string body, string Header)
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);

            notificationManager.Notify(0, notificationBuilder.Build());
        }
        else
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent)
                        .SetChannelId(Utils.CHANNEL_ID);

            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return;
            }

            var channel = new NotificationChannel(Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.High)
            {
                Description = "Firebase Cloud Messages appear in this channel"
            };

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.CreateNotificationChannel(channel);

            notificationManager.Notify(0, notificationBuilder.Build());
        }
    }
0 голосов
/ 01 апреля 2019

Я не знаю, какова ваша настоящая реализация Firebase, но это может вам помочь.

В Xamarin Forms есть хороший пакет для Firebase, который мы используем в нашем производственном приложении, разработанном командой CrossGeeks. Он отлично работает и имеет все handlers для ваших нужд. Это работает с iOS и Android, и вам не нужно писать код для конкретной платформы, только конфигурацию и некоторый код в AppDelegate.cs и MainActivity.cs

https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/FirebaseNotifications.md#notification-events

Я написал простой PushNotificationService, который обрабатывает автоматическое обновление и / или загружает новые страницы с учетом данных push-уведомлений.

Когда приложение закрывается и пользователь нажимает на уведомление, я сохраняю данные push-уведомления, используя Akavache .

 CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) =>
            {
                if (App.AppBeenResumed)
                {
                    await BlobCache.UserAccount.InsertObject("pushNotifData", p.Data);
                }
                else
                {
                    await ProcessReceivedPushNotification(p.Data);
                }
            }; 

И на целевой странице приложения я проверяю, существуют ли в методе OnAppearing страницы существующие данные push-уведомлений.

protected override void OnAppearing()
        {
            base.OnAppearing();
            App.AppBeenResumed = false;
            HandlePushNotificationIfExists();
        }

 private async void HandlePushNotificationIfExists()
        {
            IDictionary<string, object> pushNotifData;
            try
            {
                pushNotifData = await BlobCache.UserAccount.GetObject<IDictionary<string, object>>("pushNotifData");
            }
            catch (KeyNotFoundException)
            {
                pushNotifData = null;
            }

            if (pushNotifData == null) return;
            await BlobCache.UserAccount.InvalidateAllObjects<IDictionary<string, object>>();
            await PushNotificationService.ProcessReceivedPushNotification(pushNotifData);
        }

В ProcessReceivedPushNotification вы можете делать все, что захотите ... прямо нажимать на страницу или как угодно ... вызывать другую службу, которая выполнит работу по переносу новой страницы и некоторого бизнес-процесса.

Обратите внимание, что App.AppBeenResumed является статическим логическим параметром, определяющим, было ли приложение запущено или возобновлено для правильной обработки процесса обработки push-уведомления (обработайте его мгновенно или сохраните в blobcache для его обработки). позже, когда появится целевая страница).

In MainActivity.cs:

 protected override void OnCreate(Bundle bundle)

        {
           ...
           LoadApplication(new App(true));
        }

В App.cs:

 public App(bool beenResumedOrStarted)
        {
            ...
            AppBeenResumed = beenResumedOrStarted;
            ...
        }

    protected override void OnResume()
    {
        AppBeenResumed = false;
    }


    protected override void OnSleep()
    {
        //iOS states are not the same so always false when device is iOS
        AppBeenResumed = Device.RuntimePlatform != Device.iOS;
    }
...