当用户点击导航时打开通知列表页面 [英] Open Notification list page when user clicks on navigation

查看:30
本文介绍了当用户点击导航时打开通知列表页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.forms 在 android 中构建应用程序.

I'm building an app in android using Xamarin.forms.

现在我已经实现了 GCM 服务来获取通知.在这里,当用户点击通知时,我想在我的应用程序中打开一个页面.

Now I have implemented GCM Services to get notification. And here I want to open a page in my application when user clicks on the notification.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

在您的 GCMService 中,我有这个.我将 customParam 作为通知的一部分发送,这有助于您将其与其他通知区分开来.

In your GCMService I have this. I send the customParam as part of the notification, that helps you differentiate it from other notifications.

protected override void OnMessage(Context context, Intent intent){Log.Info(Tag, "GCM Message Received!");

protected override void OnMessage(Context context, Intent intent) { Log.Info(Tag, "GCM Message Received!");

var message = intent.Extras.Get("msg").ToString();
var customParam = "";

if (intent.Extras.ContainsKey("customParam"))
{
    customParam = intent.Extras.Get("customParam").ToString();
}

// This is a custom class I use to track if the app is in the foreground or background
if (Platform.StatusTracker.InView)
{
    // In foreground, hence take over and show my internal toast notification instead
    // Show Toast
}
else
{
    CreateNotification("", message, customParam);
}

}

private void CreateNotification(string title, string desc, string customParam){//创建通知var notificationManager = GetSystemService(NotificationService) as NotificationManager;

private void CreateNotification(string title, string desc, string customParam) { // Create notification var notificationManager = GetSystemService(NotificationService) as NotificationManager;

// Create an intent to show UI
var uiIntent = new Intent(this, typeof(MainActivity));

uiIntent.PutExtra("customParam", customParam);

// Create the notification
var builder = new NotificationCompat.Builder(this);
 Notification notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, uiIntent, 0))
.SetSmallIcon(Android.Resource.Drawable.SymActionEmail).SetTicker(desc)
.SetAutoCancel(true).SetContentTitle(title)
.SetContentText(desc).Build();

// Auto cancel will remove the notification once the user touches it
notification.Flags = NotificationFlags.AutoCancel;

// Show the notification
if (notificationManager != null) notificationManager.Notify(1, notification);

}

在你的 MainActivity.cs 中添加这个

In your MainActivity.cs add in this

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if (data.HasExtra("customParam"))
    {
        var customParam = data.GetStringExtra("customParam");
        if (!String.IsNullOrEmpty(customParam))
        {
            data.RemoveExtra("customParam");
            // Do your navigation or other functions here
         }
     }
}

并且基于 customParam,您可以移动到您选择的导航页面.由于您使用的是表单,因此请使用依赖注入导航服务来为您处理.

And based on the customParam you can move to the navigation page of your choice. Since you are using forms, use your Dependency Injected navigation service to handle that for you.

这篇关于当用户点击导航时打开通知列表页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆