当用户点击导航时打开通知列表页面

Open Notification list page when user clicks on navigation

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

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

我怎样才能做到这一点?

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

protected override void OnMessage(Context context, Intent 意图) { Log.Info(标签, "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(字符串标题,字符串描述,字符串自定义参数) { // 创建通知 var notificationManager = GetSystemService(NotificationService) 作为 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中加入这个

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

并且可以根据自定义参数移动到您选择的导航页面。由于您使用的是表单,因此请使用依赖注入导航服务为您处理。