Xamarin Android应用程序在收到远程通知时自动打开

Xamarin Android应用程序在收到远程通知时自动打开,android,firebase,xamarin,xamarin.android,firebase-cloud-messaging,Android,Firebase,Xamarin,Xamarin.android,Firebase Cloud Messaging,更新3:已解决 问题与FirebaseService、设置或负载无关。问题出现在应用程序代码中。。。我们的代码有这样的东西(从应用程序继承): 在OnCreate方法中的该类中,选择要使用的活动并调用“StartActivity”。我把这个开关转到MainActivity,问题就解决了 更新2: Android清单(已删除的服务标签): 结果相同:应用程序在远程通知后自动打开:( 更新: 我在onMessageReceive方法中将intent与我的主要活动、默认声音和毫秒作为int添加为通知编

更新3:已解决

问题与FirebaseService、设置或负载无关。问题出现在应用程序代码中。。。我们的代码有这样的东西(从应用程序继承):

在OnCreate方法中的该类中,选择要使用的活动并调用“StartActivity”。我把这个开关转到MainActivity,问题就解决了

更新2:

Android清单(已删除的服务标签):

结果相同:应用程序在远程通知后自动打开:(

更新:

我在onMessageReceive方法中将intent与我的主要活动、默认声音和毫秒作为int添加为通知编号(您可以在下面的代码中看到)。 我试图删除

<service android:name=".MyFirebaseMessagingService" android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
我尝试更改清单值,尝试模拟器、设备,我发送了不同的有效载荷:

通知和数据:

{
"to" : "token",
"notification" : {
"body" : "First Notification",
"title": "Collapsing A"
},
"data" : {
"body" : "First Notification",
"title": "Collapsing A",
}
}
唯一通知:

{
"to" : "token",
"notification" : { 
"body" : "First Notification", 
"title": "Collapsing A" 
}
}
仅数据:

{
"to" : "token", 
"data" : {
"body" : "First Notification",
"title": "Collapsing A"
}
}
在所有情况下,通知都是在应用程序关闭时自动接收和打开应用程序。如何避免自动打开?我只需要通知。我能做些什么

nuget软件包:

Xamaring.Firebase.Messaging - 60.1142.1
Xamarin.Firebase.Code - 60.1142.1
Xamarin.Firebase.Common - 60.1142.1

在安卓6和安卓7设备(三星)中测试

您需要在此处定义活动,当您点击通知时必须打开该活动

    var intent = new Intent(this, typeof(YourActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
    //sent RemoteMessage message data to your activity if you need
    //intent.PutExtra("User", JsonConvert.SerializeObject(user));
    var pendingIntent = PendingIntent.GetActivity(this, GenerateRandom(), intent, PendingIntentFlags.OneShot);
    var notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.myIcon)
            .SetContentTitle(user.Organization)
            .SetSubText(myModuleName)
            .SetContentText(BodyText)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent)
            .Build();
    var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
    var notification = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
   //this is for sound of notification
    var mp = MediaPlayer.Create(ApplicationContext, notification);
    mp.Start();
    notificationManager.Notify(GenerateRandom(), notificationBuilder);
只为通知提供唯一的Id,以便它们不会重叠

private int GenerateRandom()
{
    var rn = new Java.Util.Random();
    int n = 1000 - 1233 + 1;
    int i = rn.NextInt() % n;
    return 1000 + i;
}

我对这种情况的理解告诉我,由于您没有给通知一个待定的意图,它直接打开了您的应用程序:

 ParentDroidApplication : Application
  NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(RandomGenerator(), notificationBuilder.Build());
此问题的解决方法:

 Intent intent = new Intent(this, typeof(MainAcitivity)); //Activity you want to open
 intent.AddFlags(ActivityFlags.ClearTop);
 var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), intent, PendingIntentFlags.OneShot);
如果需要默认声音:

 Notification notify = new Notification();
 notify.Defaults = NotificationDefaults.Sound;
 notify.Defaults = NotificationDefaults.Vibrate;
使用NotificationCompat推送通知(用于向后兼容)

然后通知系统已从应用程序中抛出通知:

 ParentDroidApplication : Application
  NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(RandomGenerator(), notificationBuilder.Build());
此外,还可以使用随机数,以便在抛出新通知时通知不会切换位置:

private int RandomGenerator() 
{
 return new Random().Next( int.MinValue, int.MaxValue );
}
如您所见,为了实现Android Oreo频道兼容性,您需要在应用程序组件的
AndroidManifest.xml
中添加以下元数据元素:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>


当通知消息没有指定频道,或者应用程序尚未创建提供的频道时,将使用此默认频道。

感谢您的回答,但它没有帮助。当远程通知到达Hanks for answer时,应用程序仍在打开,但没有帮助。当远程通知到达时,应用程序仍在打开检查您对文件所做的更改,如果可能,请自行更新问题否我希望您使用您在firebase服务中所做的更改来更新您的问题您不应该在menifest中使用last&tag,因为您已经在
MyFirebaseMessagingService
PG的顶部按语法进行了更改。请尝试添加
“内容可用”:正确,“优先级”:“高”“,
在您的json对象中。感谢您的输入-在建议的更改后,通知仍在工作,但在远程通知后,应用程序仍将自动打开。还有一件事,我建议您尝试在
SendNotification
中放置一些其他活动,而不是登录活动。这些活动没有
MainLauncher=true
  NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(RandomGenerator(), notificationBuilder.Build());
private int RandomGenerator() 
{
 return new Random().Next( int.MinValue, int.MaxValue );
}
<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>