【发布时间】:2016-04-17 16:29:28
【问题描述】:
我们目前正在开发适用于 iOS、Android 和 WP8 的 Xamarin Forms 移动应用,我目前正在开发适用于 Android 的通知部分。
现在我能够接收通知并将其显示给用户,当他们单击通知时,它会将它们带到应用程序,但它不像我们希望的那样工作。它不会在同一个 Activity 中继续,而是会启动一个全新的 Activity,这会丢失实际应用的整个上下文。
在我们的推送通知接收器上,我们将覆盖 OnMessage 方法,一旦有东西从我们的服务器进来,就会调用该方法,在这里我们有以下代码
protected override void OnMessage(Context context, Intent intent)
{
string message = string.Empty;
// Extract the push notification message from the intent.
if (intent.Extras.ContainsKey("message"))
{
message = intent.Extras.Get("message").ToString();
var title = "Notification:";
// Create a notification manager to send the notification.
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
Intent resultIntent = new Intent(context, typeof(MainActivity));
resultIntent.PutExtras(intent.Extras);
PendingIntent contentIntent = PendingIntent.GetActivity(
context,
0,
new Intent(),
PendingIntentFlags.UpdateCurrent
);
// Create the notification using the builder.
var builder = new Notification.Builder(context);
builder.SetAutoCancel(true);
builder.SetContentTitle(title);
builder.SetContentText(message);
builder.SetSmallIcon(Resource.Drawable.icon);
builder.SetContentIntent(contentIntent);
builder.SetExtras(intent.Extras);
var notification = builder.Build();
// Display the notification in the Notifications Area.
notificationManager.Notify(0, notification);
}
}
在 MainActivity.cs 中,当用户按下通知时,我能够从通知中捕获数据,但这会创建一个新活动,而不是在当前活动中继续(PendingIntentFlags.UpdateCurrent 应该定义)。
我想要做的基本工作是在 Android 上接收通知的方式与在 iOS 上接收通知的方式基本相同,它基本上只是在应用程序的根目录中调用一个委托,然后将信息发送到应用程序本身.
我自己对 Android 几乎没有经验,而且我的大多数 Google 搜索都没有显示在按下通知时执行代码并加载其数据的任何方式,只是展示了如何在不做任何事情的情况下创建通知.
编辑:
问题已解决,方法如下
在 MainActivity.cs 中,我将 LaunchMode = LaunchMode.SingleTop 添加到了 Activity 属性中,并像这样覆盖了 OnNewIntent 方法
protected override void OnNewIntent(Intent intent)
{
string json = intent.Extras.GetString("payload");
json = HttpUtility.UrlDecode(json).Replace("\\", "");
PushReceiver.HandlePush(json, true);
base.OnNewIntent(intent);
}
在我的 PushBroadcastReceiver 中,我将 OnMessage 方法更改为
protected override void OnMessage(Context context, Intent intent)
{
string message = string.Empty;
// Extract the push notification message from the intent.
if (intent.Extras.ContainsKey("message"))
{
message = intent.Extras.Get("message").ToString();
var title = "Notification:";
// Create a notification manager to send the notification.
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
Intent resultIntent = new Intent(context, typeof(MainActivity));
resultIntent.PutExtras(intent.Extras);
PendingIntent resultPendingIntent =
PendingIntent.GetActivity(
context,
0,
resultIntent,
PendingIntentFlags.UpdateCurrent
);
// Create the notification using the builder.
var builder = new Notification.Builder(context);
builder.SetAutoCancel(true);
builder.SetContentTitle(title);
builder.SetContentText(message);
builder.SetSmallIcon(Resource.Drawable.icon);
builder.SetContentIntent(resultPendingIntent);
var notification = builder.Build();
// Display the notification in the Notifications Area.
notificationManager.Notify(new Random((int)(DateTime.Now.ToFileTime() % int.MaxValue)).Next(), notification);
}
}
由于 'LaunchMode = LaunchMode.SingleTop' 和 'PendingIntentFlags.UpdateCurrent' 不再重新创建 MainActivity 但每次用户单击通知时都会调用 OnNewIntent 事件,当捕获 OnNewIntent 事件时,您有通过 App.Current 完全访问应用程序(并将其转换为必要的页面/视图/类),因为没有创建新的 Activity,它还确保通知正常工作,而不会因重新创建 Activity 而导致故障。
谢谢Jon Douglas!
【问题讨论】:
-
如果 MainActivity 不是当前视图会怎样?
-
在我们的例子中,这是不可能发生的(整个应用程序依赖于主活动)所以我不知道,但我最好的猜测是推送将被发送到活动集在 Push Builder 中
标签: android ios xamarin notifications