Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific view not opening on notification tap when app is terminated

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.

like image 975
R15 Avatar asked Dec 03 '18 13:12

R15


People also ask

Why do I only get notifications when I open the app?

Make sure you haven't turned off app notifications. If you aren't receiving notifications from a specific app, the most likely culprit is the notification settings for that app. Every app has its own set of permissions to access Android features, and notifications are one.

Does an app need to be open for push notifications?

The basics of push notifications A push notification is delivered instantly to a mobile device, regardless of whether the device is locked or unlocked. The push notification is delivered even if the user is in a different app or away from the app sending the push notification.


1 Answers

Well, I am not really sure where the issue might be. However, if we take a look at the docs: https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/GettingStarted.md, there are several things that you can try.

First of all set Exported = true and LaunchMode = LaunchMode.SingleTop in your MainActivity. Also set FirebasePushNotificationManager.ProcessIntent(this, Intent); in your onCreate(), right after LoadApplication(new App());.

Note that as of Android 8.0, you have to set DefaultNotificationChannelId and DefaultNotificationChannelName in your Application's onCreate() as well.

Then, if you have a SplashActivity, make sure you add MainLauncher = true, NoHistory = true to it, and the following code in onCreate():

var mainIntent = new Intent(Application.Context, typeof(MainActivity));

 if (Intent.Extras != null) {
     mainIntent.PutExtras(Intent.Extras);
 }
 mainIntent.SetFlags(ActivityFlags.SingleTop);
 StartActivity(mainIntent);

I would also recommend you to take a look at the following folder of the repo: https://github.com/CrossGeeks/FirebasePushNotificationPlugin/tree/master/samples/FirebasePushSample/FirebasePushSample.Android. If you carefully follow all code, it should work.

like image 59
gi097 Avatar answered Oct 14 '22 05:10

gi097