【问题标题】:How to open a Content page after FCM notification click on Xamarin Forms?FCM 通知单击 Xamarin 表单后如何打开内容页面?
【发布时间】:2018-05-10 02:38:17
【问题描述】:

我已经在 xamarin 表单上实现了 FCM 通知。所以我想要,当用户点击通知时,它将打开一个 Xamarin 表单内容页面。 这是我接收 FCM 通知的代码: 在 MyFireMessagingService 类内部-

public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);
            SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
        }
        public void SendNotificatios(string body, string Header)
        {
            Notification.Builder builder = new Notification.Builder(this);
            builder.SetSmallIcon(Resource.Drawable.AppLauncher);
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
            builder.SetContentIntent(pendingIntent);
            builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.AppLauncher));
            builder.SetContentTitle(Header);
            builder.SetContentText(body);
            builder.SetDefaults(NotificationDefaults.Sound);
            builder.SetAutoCancel(true);
            NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(1, builder.Build());
        }

那么点击通知后如何打开内容页面?

【问题讨论】:

  • 好问题,使用 Xamarin Android,您只需从使用 Intent.GetStringExtra 指示的 Activity 中添加额外的 Intent 来处理它,但这里只有一个 Activity ......我想那里就是像Montemagno解决权限问题一样解决(github.com/jamesmontemagno/PermissionsPlugin),我担心我不够专家。
  • PermissionsPlugin 不是解决这个问题的解决方案
  • 我并不是说它解决了这个问题,只是说方法可能相似。因为理论上你也必须在每个 Activity 中处理权限,而在 Xamarin Forms 中只有一个。

标签: xamarin xamarin.forms


【解决方案1】:

作为我替换的第一件事

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

public override void HandleIntent(Intent intent)
    {
        CreateNotification(intent);
    }

然后新建方法CreateNotification如下,

private void CreateNotification(Object e)
    {
        try
        {
            string title = "";
            string body = "";

            var intent = new Intent(this, typeof(MainActivity));

            var i = e as Intent;
            var bundle  = i.Extras;
            title = bundle.GetString("gcm.notification.title");
            body = bundle.GetString("gcm.notification.body");


            intent.PutExtra("title", title);
            intent.PutExtra("body", body);

            intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop );
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent | PendingIntentFlags.UpdateCurrent);
            Notification.Builder builder = new Notification.Builder(this);
            builder.SetSmallIcon(Resource.Drawable.AppLauncher);
            builder.SetContentIntent(pendingIntent);
            builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.AppLauncher));
            builder.SetContentTitle(Header);
            builder.SetContentText(body);
            builder.SetDefaults(NotificationDefaults.Sound);
            builder.SetAutoCancel(true);
            NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(1, builder.Build());

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

现在我们必须更新 MainActivity.cs 以通过覆盖 OnNewIntent 方法来处理推送通知点击,如下所示

protected async override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        var title = intent.GetStringExtra("title");
        //This method will get called while the app is launching from the app icon or from the notification
        if (title != null)
        {
            //Means new Intent from push notification
            //Code to open the page
        }
    }

确保在活动中保留 LaunchMode = LaunchMode.SingleTask

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 2019-08-10
    • 1970-01-01
    • 2016-12-22
    相关资源
    最近更新 更多