当前位置: 首页 > 知识库问答 >
问题:

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

夔光霁
2023-03-14

我已经在我的应用程序上实现了推送通知,它们工作得很好。我遇到的问题是,当我点击下拉菜单中的它们时,它们会立即重新加载应用程序。为了解决这个问题,我让应用程序创建了一个新的活动实例。这现在打开了一个新的页面,但当点击从这个新的页面返回时,它有同样的问题,并重新加载整个应用程序。

 [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());
    }


}
  [Activity(Label = "Notifications")]
public class SecondActivity:Activity
{

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        System.Diagnostics.Debug.WriteLine("this worked");
    }

}

共有1个答案

长孙阳嘉
2023-03-14

你可以查看我的帖子,它展示了我是如何做到的,我还打开了一个表单页面。这篇文章有点长,但基本上有几件事:

  • 我不使用第二个活动
  • 我不将cleartop标志添加到mainactivityintent
  • 我不使用TaskStackBuilder,而是只使用PendingIntent.GetActivity获取PendingIntent
  • 然后,我在文章中通过将MainActivityLaunchMode设置为LaunchMode.SingleTop,重写MainActivity中的OnNewIntent,然后检查Intent.HasExtras中设置的特殊String,以将该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);

    if (Forms.IsInitialized) { // Ensure XF code has been initialized
        // 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());
}

则在MainActivity中:

[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);
    }
 类似资料:
  • 我已经为我的Xamarin.Forms UWP应用程序实现了一个推送通知功能,我可以接收通知,然后弹出一个祝酒词。我正在使用下面的代码。 当用户单击/轻击通知时,我想从我的PCL项目中打开一个特定的页面,并将这个变量作为参数传递。我怎么能这么做?如有任何帮助,我们将不胜感激。

  • 我有一个使用Xamarin.Forms的应用程序,针对IOS,Android和WP 8。 我的应用中需要推送通知功能。 我看过<code>pushsharp</code>演示,它似乎很有前途。但我看到的所有代码都是针对每个平台分别编写的。 我希望这件事能在法庭上进行。Forms项目,在App.cs中的某个地方,这样我就不需要重复注册设备的代码,也不需要处理应该如何处理推送通知。 任何帮助都将不胜感

  • 我想使用按钮单击中的GCM将推送通知从一个设备发送到多个设备。我遵循了GCM的所有流程。我获得了设备的服务器密钥和注册ID,但没有使用GCM获得推送通知。我也在谷歌上搜索过,但没有找到正确的解决方案。 请建议我如何在多设备上发送推送通知。 MAYActivity.java

  • 当应用程序在后台时,如何更新包含数据负载的Firebase推送通知?有没有办法在通知中指定通知id给Firebase API? 完整的项目在github https://github.com/akshatashan/FireBaseCloudMessagingDemo中

  • 我有以下代码。当用户在后台或关闭应用程序时点击推送通知时,MainActivity。OnNewIntent被调用,但意图缺少处理推送通知时放入的额外内容。如果单击通知时应用程序处于前台,则intent参数具有额外功能,并得到正确处理。 我的MainActivity的OnNewIntent方法如下: 当应用程序打开时用户单击推送通知时,我们点击日志消息。当用户在应用关闭时或在后台单击时,不会因为意图

  • 我想用Xamarin格式的C#创建一个基于文本的Android游戏。 在故事中,我想设置角色任务,这需要一些时间,例如“我去挖这个洞,完成后给你打电话。” 如何将通知设置为在设置的时间之后显示?例如,上述声明可能需要10分钟,然后用户收到继续游戏的通知? 我一周前才开始做C#,所以如果这是noobish,或者已经被问到了,我道歉。我到处都找过,但有几种类型的通知,当我试图理解它时,似乎我在读法语。