应用程序重新加载推送通知单击Xamarin表单

时间:2017-06-28 14:30:03

标签: c# xamarin xamarin.forms

我已经在我的应用上实施了推送通知,但它们运行正常。 我遇到的问题是,当我在下拉菜单中单击它们时,它们会立即重新加载应用程序。为了解决这个问题,我让app创建了一个新的活动实例。现在这会打开一个新页面,但是当从这个新页面单击回来时,它会遇到同样的问题并再次重新加载整个应用程序。

我正在使用Xamarin Forms和PCL,所以它不是纯粹的Android。 有没有办法让菜单项上的click事件加载PCL中的特定页面视图?重新加载整个应用程序是没用的。

这是创建电话通知的类:

ForegroundMessages.cs:

create table #t1 (c1 int not null,c2 int null)

  select CONVERT(xml,(select *
    from #t1
    where 0=1
    for xml raw,elements xsinil,XMLSCHEMA('XXX')
    ))

和第二个Activity类:

 [Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]

public class ForegroundMessages: FirebaseMessagingService
{


    const string TAG = "MyFirebaseMsgService";
    public override void OnMessageReceived(RemoteMessage message)
    {
        //Log.Debug(TAG, "From: " + message.From);
        //Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
        base.OnMessageReceived(message);
        SendNotification(message.GetNotification().Body);
    }

    private void SendNotification(string body)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
       // var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        Log.Debug(TAG, "Notification Message Body: " + body);

        // sends notification to view model
        MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Increase");

        Intent resultintent = new Intent(this,typeof(SecondActivity));

        TaskStackBuilder stackbuilder = TaskStackBuilder.Create(this);
        stackbuilder.AddParentStack(Java.Lang.Class.FromType(typeof(SecondActivity)));
        stackbuilder.AddNextIntent(resultintent);

        PendingIntent resultPendingIntent = stackbuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

        var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
        var notificationBuilder = new NotificationCompat.Builder(this)
            .SetSmallIcon(Resource.Drawable.icon)
            .SetContentTitle("Notification")
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetSound(defaultSoundUri)
            .SetContentIntent(resultPendingIntent);

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }


}

如上所述,这可以正常工作并接收消息,当您单击它们时,它会加载第二个活动,但当离开第二个活动页面时,它只会重新加载整个应用程序。 如果我可以在PCL中加载一个视图页面(.xaml或.xaml.cs)而不是它,那会更好。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您可以查看my post,其中显示了我是如何做到的,我还打开了一个表单页面。帖子有点长,但基本上有一些东西:

  • 我不使用第二个Activity
  • 我没有将ClearTop标记添加到MainActivity Intent
  • 我不使用TaskStackBuilder而只是使用PendingIntent.GetActivity来获取PendingIntent
  • 然后,我将MainActivity LaunchMode LaunchMode.SingleTop设置为OnNewIntent,覆盖MainActivity intent.HasExtras,从而专门解决帖子中的应用重新启动问题然后检查string您在SendNotification方法中设置的特殊Intent,以区分Intent与其他private void SendNotification(string body) { var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); // var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); intent.PutExtra("SomeSpecialKey", "some special value"); Log.Debug(TAG, "Notification Message Body: " + body); // sends notification to view model MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Increase"); PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot); var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification); var notificationBuilder = new NotificationCompat.Builder(this) .SetSmallIcon(Resource.Drawable.icon) .SetContentTitle("Notification") .SetContentText(body) .SetAutoCancel(true) .SetSound(defaultSoundUri) .SetContentIntent(pendingIntent); var notificationManager = NotificationManager.FromContext(this); notificationManager.Notify(0, notificationBuilder.Build()); }

如果您之后仍有问题,请告诉我。

编辑:

[Activity(LaunchMode = LaunchMode.SingleTop, ....)]
public class MainActivity : FormsApplicationActivity {

    protected override void OnNewIntent(Intent intent) {
        if(intent.HasExtra("SomeSpecialKey")) {
            System.Diagnostics.Debug.WriteLine("\nIn MainActivity.OnNewIntent() - Intent Extras: " + intent.GetStringExtra("SomeSpecialKey"); + "\n");
        }

        base.OnNewIntent(intent);
    }

然后在MainActivity中:

tar czvf /tmp/empty.tgz --from-file /dev/null
tar: Option --from-file is not supported

答案 1 :(得分:0)

只需将LaunchMode = LaunchMode.SingleTask添加到您的MainActivity

相关问题