Arv*_*iya 7 android firebase xamarin xamarin.forms firebase-cloud-messaging
我在工作的推送通知Xamarin.Forms使用Plugin.FirebasePushNotification.当应用终止(从任务栏中杀死)时,通知不会打开特定视图.
当应用程序打开或在后台打开时,我可以在单击通知时导航到特定页面.这是Application类
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
共享项目中的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"]}")
});
});
}
};
}
Run Code Online (Sandbox Code Playgroud)
这是我从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",
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的宣言课
<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>
Run Code Online (Sandbox Code Playgroud)
我也在GitHub上分配了这个问题.谢谢.
嗯,我不太确定问题可能出在哪里。但是,如果我们看一下文档:https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/GettingStarted.md,您可以尝试以下几种操作。
首先设置Exported = true并LaunchMode = LaunchMode.SingleTop在你的MainActivity. 也定FirebasePushNotificationManager.ProcessIntent(this, Intent);在你的onCreate(),就在之后LoadApplication(new App());。
请注意,从 Android 8.0 开始,您还必须在应用程序中设置DefaultNotificationChannelId和。DefaultNotificationChannelNameonCreate()
然后,如果您有一个SplashActivity,请确保添加MainLauncher = true, NoHistory = true到其中,并在 中添加以下代码onCreate():
var mainIntent = new Intent(Application.Context, typeof(MainActivity));
if (Intent.Extras != null) {
mainIntent.PutExtras(Intent.Extras);
}
mainIntent.SetFlags(ActivityFlags.SingleTop);
StartActivity(mainIntent);
Run Code Online (Sandbox Code Playgroud)
我还建议您查看存储库的以下文件夹:https://github.com/CrossGeeks/FirebasePushNotificationPlugin/tree/master/samples/FirebasePushSample/FirebasePushSample.Android。如果您仔细遵循所有代码,它应该可以工作。
| 归档时间: |
|
| 查看次数: |
1578 次 |
| 最近记录: |