Android Xamarin使推送通知不创建新活动,而是使用当前活动
我们目前正在为iOS、Android和WP8开发Xamarin格式的移动应用程序,我目前正在为Android开发通知部分 现在我可以接收通知并向用户显示,当用户单击通知时,它会将他们带到应用程序,但它不能像我们希望的那样工作。它不会在同一个活动中继续,而是启动一个全新的活动,从而丢失实际应用程序的整个上下文 在我们的推送通知接收器上,我们覆盖了OnMessage方法,该方法在从我们的服务器传入内容时立即调用get,在这里我们有以下代码Android Xamarin使推送通知不创建新活动,而是使用当前活动,android,ios,xamarin,notifications,Android,Ios,Xamarin,Notifications,我们目前正在为iOS、Android和WP8开发Xamarin格式的移动应用程序,我目前正在为Android开发通知部分 现在我可以接收通知并向用户显示,当用户单击通知时,它会将他们带到应用程序,但它不能像我们希望的那样工作。它不会在同一个活动中继续,而是启动一个全新的活动,从而丢失实际应用程序的整个上下文 在我们的推送通知接收器上,我们覆盖了OnMessage方法,该方法在从我们的服务器传入内容时立即调用get,在这里我们有以下代码 protected override void OnMess
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中,当用户按下通知时,我能够捕捉到通知中的数据,但这会创建一个新的活动,而不是在当前活动中继续(PendingEventFlags.UpdateCurrent应该定义该活动)
我想做的基本工作是,在Android上接收通知的方式基本上与在iOS上接收通知的方式相同,在iOS上,它基本上只调用应用程序根目录中的委托,然后将信息发送到应用程序本身
我自己对安卓几乎没有经验,我的大多数谷歌搜索都没有显示在按下通知并加载其数据时执行代码的任何方式,只是显示了如何创建通知,而不需要它做很多事情
编辑:
这个问题已经解决了,下面是如何解决的
在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”和“PendingEventFlags.UpdateCurrent”的原因,不再重新创建主活动get,但每次用户单击通知时都会调用OnNewIntent事件,当捕捉到OnNewIntent事件时,您可以通过app.Current完全访问应用程序(并将其强制转换到必要的页面/视图/类)因为没有创建新的活动,它还确保通知工作时不会出现因重新创建活动而导致的故障
谢谢!我相信您需要在您的
活动中使用singleTop
,这里:
android:launchMode=“singleTop”
此外,这些意图标志也有助于:
谢谢!singleTop帮了大忙,再加上一些谷歌搜索,我可以在主活动中调用OnNewIntent通知,而不是创建一个新的通知,推送通知系统现在可以正常工作了:D,我今天晚些时候会更新上面的代码以显示工作版本。我正在尝试做同样的事情。设置后tingandroid:launchMode=“singleTop”
并将singleTop添加到intent它不起作用我已经用我的工作代码更新了帖子,你可以看一下。我也没有在intent上使用singleTop,但我使用UpdateCurrent,这可能就是这里的区别。如果MainActivity不是当前视图,会发生什么?在我们的情况下,不可能发生这种情况(整个应用程序依赖于主活动)所以我不知道,但我最好的猜测是推送将发送到推送生成器中设置的活动