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

时间:2015-10-17 12:17:20

标签: xamarin.forms

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

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

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

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

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(字符串标题,字符串desc,字符串customParam) {     //创建通知     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中添加

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,您可以移动到您选择的导航页面。由于您使用的是表单,请使用Dependency Injected导航服务为您处理。