Xamarin表单:点按推送通知android时加载内容页面

问题描述 投票:0回答:2

我完成了从FCM控制台接收测试通知。现在我在点击通知时尝试打开页面。关于如何实现这一点的任何想法?我搜索过互联网但找不到合适的解决方案。我也可以通过邮递员发送通知。

xamarin.forms push-notification tap
2个回答
0
投票

我不知道你的实际Firebase实现是什么,但这可能会对你有所帮助。

我们在CrossGeeks团队制作的App中使用了Xamarin Forms中的Firebase。它工作得很好,并有所有handlers满足您的需求。这适用于iOS和Android,您不需要编写特定于平台的代码,只需配置和AppDelegate.csMainActivity.cs中的一些代码

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

我写了一个简单的PushNotificationService来处理自动刷新和/或推送新页面,考虑推送notif数据。

当应用关闭并且用户点击通知时,我使用Akavache存储推送通知数据。

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

在应用程序的登录页面上,我检查页面的OnAppearing方法中是否存在push notif数据。

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是一个静态bool,用于确定App是否已启动或恢复以正确处理push notif的处理过程(立即处理它或将其存储在blobcache中以便在登陆页面出现时对其进行处理)。

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;
    }

0
投票

我按照以下方式处理通知。页面加载在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));
}

在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());
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.