Specific view not opening on notification tap when app is terminated

时间:2018-12-03 13:07:55

标签: android firebase xamarin xamarin.forms firebase-cloud-messaging

I am working on push notifications in Xamarin.Forms using Plugin.FirebasePushNotification. Notifications are not opening specific view when app terminated(killed from taskbar).

When app open or in background, I am able to navigate to specific page when clicking on notification. This is Application class

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 class

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 class in shared project

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"]}")
            });
        });
        }
    };
}

This is payload I am sending from 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",
}
}

This is my menifest class

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

I have assigned this issue on GitHub as well. Thank you.

3 个答案:

答案 0 :(得分:0)

如果有的话,请先在主要活动之前进行活动,然后在简历上将推送信息发送到主要活动,示例如下:

protected override void OnResume()
    {
        base.OnResume();
        Task startupWork = new Task(() => { SimulateStartup(); });
        startupWork.Start();
    }

    // Simulates background work that happens behind the splash screen
    async void SimulateStartup()
    {

        var intent = new Intent(this, typeof(MainActivity));
        if (Intent.Extras != null)
        {
            intent.PutExtras(Intent.Extras); // copy push info from splash to main
        }
        StartActivity(intent);
    }

在主要活动的新intent方法中,您可以获得详细信息。下面的示例,

protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        var type = intent.GetStringExtra("type");
        if ((!string.IsNullOrEmpty(type) && object.Equals(type, "notification")))
        {
            // do your logic

        }
    }

答案 1 :(得分:0)

如果应用在后台被杀死或未被杀死,则在这种情况下不会调用OnNewIntent Mentod。仅当现有屏幕位于背景中时(即,现有意图位于背景中)并且您激发新意图将屏幕向前移动时才调用它(我可能没有使用正确的技术用语,只是试图解释这个概念)

在您的情况下,由于应用程序不在后台,因此调用OnCreate,因此您需要从那里处理它,

尝试将流程意图调用移至oncreate中的LoadApplication方法下方。

例如:

LoadApplication(new App());
FirebasePushNotificationManager.ProcessIntent(this,Intent)

-或-

我建议解析OnCreate方法中的意图,并在每次单击通知时通过构造函数将必要的信息作为参数传递给APP类。

例如:LoadApplication(new App('imp data that you want'));

从App类中,您可以将用户导航到所需的任何位置,而不会干扰应用程序的后堆栈。

答案 2 :(得分:0)

嗯,我不太确定问题可能在哪里。但是,如果我们看一下文档https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/GettingStarted.md,则可以尝试几种方法。

首先在您的Exported = true中设置LaunchMode = LaunchMode.SingleTopMainActivityFirebasePushNotificationManager.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);

我还建议您查看以下回购文件夹:https://github.com/CrossGeeks/FirebasePushNotificationPlugin/tree/master/samples/FirebasePushSample/FirebasePushSample.Android。如果您仔细遵循所有代码,它应该可以工作。