我们目前正在为 iOS、Android 和 WP8 开发 Xamarin Forms 中的移动应用程序,而我目前正在为 Android 开发通知部分。
现在我能够接收通知并将它们显示给用户,当他们点击通知时,它会将他们带到应用程序,但它无法像我们希望的那样工作。它不是在同一个 Activity 中继续,而是启动一个全新的 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 中,我能够在用户按下通知时从通知中捕获数据,但这会创建一个新 Activity ,而不是继续当前 Activity (PendingIntentFlags.UpdateCurrent 应该定义)。
我想做的事情的基础基本上是在 Android 上接收通知的方式与我们在 iOS 上接收通知的方式基本相同,它基本上只是在应用程序的根目录中调用一个委托(delegate),然后将信息发送到应用程序本身.
我自己对 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 完全访问应用程序(并将其转换到必要的页面/ View /类),因为没有创建新的 Activity ,它还确保通知正常工作,不会因重新创建 Activity 而引起的故障。
谢谢 Jon Douglas !
最佳答案
我认为您需要在此处的 Activity
中使用 singleTop
:
android:launchMode="singleTop"
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
此外,这些 Intent 标志也可能有帮助:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP
关于Android Xamarin 使推送通知不创建新 Activity 而是使用当前 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34754149/