Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Firebase Messaging (FCM) OnMessageReceived Running Twice

$
0
0

Good day

I am using Firebase Messaging to send/Receive Push Notification on Android.

I've got the following implementation which is triggering OnMessageReceived twice on Android v 8.0, after rebooting the phone once, opening the app, rebooting the phone again and then sending myself a push notification.

Any ideas on what is causing this? I've looked online and found suggestions on removing any old GCM references which I have done already. The first time the app is built and deployed to the device, it doesn't happen and all subsequent push notifications seem fine. This just happens when the phone goes through the above mentioned reboot procedure. It may happen in other cases too but this has been my method to re-create the issue.

The App is compiled using

  • Xamarin.Forms 3.4.0.1008975
  • Xamarin.Firebase.Messaging 60.1142.1

If any further information is required, please ask me.

The code is as follows:

MainActivity.cs

using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Gms.Common;
using Android.OS;
using Android.Views;
using Plugin.Permissions;
using Android.Util;
using Android.Gms.Common.Apis;
using Android.Gms.Drive;
using Android.Runtime;
using System;

namespace App.Droid
{
    [Activity(Label = "App", Icon = "@drawable/launch_icon", LaunchMode = LaunchMode.SingleTop, Theme = "@style/MyTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener, IResultCallback
    {
        const string TAG = "MainActivity";
        const int REQUEST_CODE_RESOLUTION = 3;
        public static GoogleApiClient googleApiClient;
        public static byte CurrentGoogleDriveState = 0x00;

        internal static readonly string CHANNEL_ID = "default";
        internal static readonly int NOTIFICATION_ID = 100;

        protected override void OnCreate(Bundle bundle)
        {

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
                LoadApplication(new App("", ""));

            IsPlayServicesAvailable();
            CreateNotificationChannel();
        }

        void CreateNotificationChannel()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                // Notification channels are new in API 26 (and not a part of the
                // support library). There is no need to create a notification
                // channel on older versions of Android.
                return;
            }

            var channel = new NotificationChannel(CHANNEL_ID,
                                                  "FCM Notifications",
                                                  NotificationImportance.Default)
            {

                Description = "Firebase Cloud Messages appear in this channel"
            };

            var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
            notificationManager.CreateNotificationChannel(channel);
        }

        public bool IsPlayServicesAvailable()
        {
            int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.Success)
            {
                return false;
            }
            return true;
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="--Redacted--" android:versionCode="--Redacted--" android:versionName="--Redacted--">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-feature android:name="android.hardware.screen.landscape" />
    <uses-feature android:name="android.hardware.wifi" android:required="false" />
    <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
    <application android:label="--Redacted--" android:icon="@drawable/logo_cropped" android:allowBackup="true" android:largeHeap="true">
        <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="android.intent.action.BOOT_COMPLETED" />
                <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="android.max_aspect" android:value="2.1" />
    </application>
</manifest>

FirebaseRegistration.cs

using Android.App;
using Firebase.Iid;
using Android.Util;
using System;
using System.IO;
using Android.Content;

namespace App.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    public class MyFirebaseIIDService : FirebaseInstanceIdService
    {
        const string TAG = "MyFirebaseIIDService";
        public override void OnTokenRefresh()
        {
            base.OnTokenRefresh();

            var refreshedToken = FirebaseInstanceId.Instance.Token;
            Log.Debug(TAG, "Refreshed token: " + refreshedToken);
            SendRegistrationToServer(refreshedToken);

        }

        /// <summary>
        /// Sends the token to server.
        /// </summary>
        /// <param name="token">Token.</param>
        void SendRegistrationToServer(string token)
        {
            // My code to send the push notification ID to our backend
        }
    }
}

FirebaseMessaging.cs

using System;
using System.Globalization;
using Android.App;
using Android.Content;
using System.Linq;
using Firebase.Messaging;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.OS;
using static Android.App.ActivityManager;
using Android.Support.V4.App;

namespace App.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";

        async public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);

            Console.WriteLine("["+this.GetType().FullName+"]\t\t\t\t Push Notification Received! \n\n" + Newtonsoft.Json.JsonConvert.SerializeObject(message));
        }
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>