【发布时间】:2019-01-30 15:34:09
【问题描述】:
更新 3:已解决!
问题与 FirebaseService、设置或负载无关。问题出在应用程序代码中......我们的代码有这样的东西(从应用程序继承):
ParentDroidApplication : Application
在该类中的 OnCreate 方法中选择要使用的活动并调用“StartActivity”。我将此开关移到 MainActivity 并解决了问题。
更新 2:
Android Manifest(已删除服务标签):
<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 中的所有消息(通知)都可以正常工作、接收甚至更新徽章,但是...... Android 存在一个问题:
如果应用程序已关闭(从应用程序列表中滑动)并收到远程通知,它将自动打开。我想避免这种情况。背景或前景模式按预期工作。
我的 Andoroid 清单:
<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>
我的 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
在 Android 6 和 Android 7 设备(三星)中测试
【问题讨论】:
-
您不应该在清单中使用最后一个
和 标记,因为您已经在 MyFirebaseMessagingService的顶部按语法使用了它。尝试在您的 json 对象中添加"content_available" : true, "priority" : "high",。 -
感谢您的输入 - 建议更改后通知仍然有效,但远程通知后应用程序仍会自动打开
-
我建议您尝试在
SendNotification中添加一些其他活动,而不是登录活动。哪个没有MainLauncher=true
标签: android firebase xamarin xamarin.android firebase-cloud-messaging