FCM通知点击Xamarin表格后如何打开内容页面?

时间:2018-03-06 07:17:56

标签: xamarin xamarin.forms

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

rbind

所以如何在点击通知后打开内容页面?

1 个答案:

答案 0 :(得分:0)

我已经取代了第一件事

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