我正在使用Plugin.FirebasePushNotification
在Xamarin.Forms
中开发推送通知。当应用程序被终止(从任务栏中关闭)时,通知无法打开特定视图。
当应用程序处于打开或后台状态时,单击通知时可以导航到特定页面。 这是应用程序类。
public class AppClass : Android.App.Application, Android.App.Application.IActivityLifecycleCallbacks
{
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(this);
FirebasePushNotificationManager.Initialize(this,false,true);
var instanceid = FirebaseInstanceId.Instance.Token;
}
}
MainActivity类
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
FirebasePushNotificationManager.ProcessIntent(this, intent);
}
共享项目中的App.xaml.cs类
protected override void OnStart()
{
CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
if (p.Data.ContainsKey("color"))
{
Device.BeginInvokeOnMainThread(() =>
{
Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new Page1()
{
BackgroundColor = Color.FromHex($"{p.Data["color"]}")
});
});
}
};
}
这是我从Postman发送的有效载荷。
{
"to":"dhNe4U_jSDo:APA91bHhupDfnqW5Y9EBIfWV-uQVmq_HyPXTzBZ_TnSfM-7cDEq8SUFDLeudA4vWzyMj28E8A_c5HS0F2f5xT8quWZHcmWL8RJEbeDNre3RMsuY7brcAxqQMQOMCDcVXWDsl7s15l-NC",
"notification" : {
"body" : "New announcement assigned",
"content_available" : true,
"priority" : "max",
"color":"Page1",
"content_available" : true,
"title": "notification TITLE",
"content_available" : true,
"body": "notification BODY",
},
"data" : {
"OrganizationId":"2",
"color":"Page1",
"click_action":"MainActivity"
"sectionId":"33",
"priority" : "high",
"title": "notification TITLE",
"content_available" : true,
"body": "notification BODY",
}
}
这是我的清单类
<application android:label="FCMPush.Android">
<uses-permission android:name="android.permission.INTERNET" />
<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>
</application>
我也在GitHub上将这个问题分配了。谢谢。
click_action
。1. 它需要在notification
下面。2. 根据文档 "如果指定,当用户点击通知时,将启动具有匹配意图过滤器的活动。" - AL.click_action
中的值有一个特定的格式。我已经有一段时间没有测试过了。你能否尝试添加活动的确切位置(例如com.sample.Activity
)--就像我在这里的答案中所做的那样? - AL.