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



更新 3:已解决!

问题未与 Firebase服务、设置或有效负载相关联。问题出在应用程序代码中...我们的代码有这样的东西(从应用程序继承(:

ParentDroidApplication : Application

在 OnCreate 方法的该类中,选择要使用的活动并调用"StartActivity"。我将此开关移至主活动,问题已解决。

更新 2:

安卓清单(已删除服务标签(:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.visma.vfsmobileparentapp" android:versionName="1.0" android:installLocation="internalOnly" android:versionCode="46">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:theme="@style/Theme.AppCompat.Light" android:label="Min skole" android:icon="@drawable/ic_launcher" android:name="Min skole">
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_launcher" />
</application>
</manifest>

有效负载(添加content_available和优先级(:

{
"to" : "token",
"collapse_key" : "type_a",
"notification" : {
"body" : "First Notification",
"title": "Collapsing A"
},
"data" : {
"body" : "First Notification",
"title": "Collapsing A"
},
"content_available": true,
"priority": "high"
}

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

更新:

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

<service android:name=".MyFirebaseMessagingService" android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

我没有收到任何通知,但应用程序仍在打开。 似乎Firebase正在正常工作。但应用它自己做魔术

我试图将Firebase降级到版本42.1021.1。结果相同

问题

我们正在使用 Xamarin 开发移动应用程序。我们需要通知,我们开始使用Firebase Cloud消息传递。所有消息(通知(都在工作,接收,甚至在iOS中更新徽章,但是...安卓的一个问题:

如果应用程序已关闭(从应用程序列表中滑动(并收到远程通知,则它会自动打开。我想避免这种情况。后台或前台模式按预期工作。

我的安多里人清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app" android:versionName="1.0" android:installLocation="internalOnly" android:versionCode="46">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:theme="@style/Theme.AppCompat.Light" android:label="App" android:icon="@drawable/ic_launcher" android:name="App">
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_launcher" />
</application>
<service android:name=".MyFirebaseMessagingService" android:stopWithTask="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</manifest>

My FirebaseMessagingService.cs:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
SendNotification("title", "body"); //just for testing
}
public void SendNotification(string title, string body)
{
var intent = new Intent(this, typeof(LoginActivity));
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
var style = new Android.Support.V4.App.NotificationCompat.BigTextStyle();
style.BigText(body);
Bitmap largeIcon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher);
var notificationBuilder =
new Android.Support.V4.App.NotificationCompat.Builder(this)
.SetLargeIcon(largeIcon)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentTitle(title)
.SetContentText(body)
.SetStyle(style)
.SetAutoCancel(true)
.SetSound(defaultSoundUri)
.SetContentIntent(pendingIntent)
.SetVisibility((int)NotificationVisibility.Public);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(DateTime.Now.Millisecond, notificationBuilder.Build());
}
}

我尝试更改清单值,尝试模拟器,设备,我发送了不同的有效负载:

通知和数据:

{
"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;
}

我对这种情况的理解告诉我,由于您没有向通知发出待处理的意图,因此它直接打开了您的应用程序:

此问题的解决方法:

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.Builder notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetSound(Settings.System.DefaultNotificationUri)
.SetVibrate(new long[] { 1000, 1000 })
.SetLights(Color.AliceBlue, 3000, 3000)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);

然后通知系统从应用程序引发通知:

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"/>

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

相关内容

  • 没有找到相关文章

最新更新