单击通知时,我的Intent.Extras始终为null。我怎样才能获得Intent数据?

时间:2013-09-04 12:55:47

标签: android android-intent notifications xamarin android-notifications

查看通知时,我无法获取Intent的Extras数据。以下是我如何构建通知以及传递给它的intent数据。它有什么问题吗?我见过其他与此相似的例子似乎有用。

    protected override void OnMessage(Context context, Intent intent)
    {
            // irrelevant stuff removed

            string title = "Notification";
            string message = "The Notification Message";

            Bundle valuesForActivity = new Bundle();
            valuesForActivity.PutInt("panelId", (int)panelId);

            Intent pendingIntent = new Intent(context, typeof (TabContainer));
            pendingIntent.PutExtras(valuesForActivity);
            pendingIntent.SetFlags(ActivityFlags.SingleTop);
            pendingIntent.PutExtra("panelId", (int)panelId);

            //neither PendingIntentFlags.CancelCurrent or PendingIntentFlags.UpdateCurrent works
            PendingIntent resultPendingIntent = PendingIntent.GetActivity(context, 0, pendingIntent, PendingIntentFlags.CancelCurrent);
            //PendingIntent resultPendingIntent = PendingIntent.GetActivity(context, 0, pendingIntent, PendingIntentFlags.UpdateCurrent);

            var builder = new NotificationCompat.Builder(context);
            builder.SetContentTitle(title);
            builder.SetAutoCancel(true);
            builder.SetSmallIcon(Resource.Drawable.icon24);
                builder.SetLargeIcon(BitmapFactory.DecodeStream(context.Resources.OpenRawResource(Resource.Drawable.icon96)));
                builder.SetContentText(message);
            builder.SetContentIntent(resultPendingIntent);
            builder.SetTicker(title);
            builder.SetVibrate(new long[] { 100, 200, 300 });

            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.Notify(1, builder.Build());
    }

然后在TabContainer活动中,我从未得到我需要的数据:

public class TabContainer : TabActivity
{
    protected override void OnResume()
    {
        base.OnResume();

        int panelId = Intent.GetIntExtra("panelId", 0); // always 0

        var extras = Intent.Extras; // always null

    }
}

1 个答案:

答案 0 :(得分:2)

这有效:

public class TabContainer : TabActivity
{
    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        try
        {
            var panelId = intent.GetIntExtra("panelId", 0);

            // do the things I need with panelId.
        }
        catch (Exception ex)
        {
            DealWithError(ex);
        }
    }
}