Home > Back-end >  Firebase Push Notification not working in Xamarin
Firebase Push Notification not working in Xamarin

Time:09-17

I watched Gerald Versluis' tutorial on Push Notifications with Xamarin.Forms (Android) and FCM. Video tutorial and source code github

I tried and downloaded the source code, change Package Name, I just replaced the google-services.json code with my generated code on Firebase Cloud Messaging. However it doesn't work, The event Current_OnTokenRefresh never fired so i dont know the Token. And of coarce there are not any notifications. Where am i wrong?

I am using emulator on Genymotion - SS Galaxy S8. There is no Google Play Store on this emulator.

CodePudding user response:

Make sure your google-services.json build action is set to GoogleServicesJson, or you can simply add the "<GoogleServicesJson Include="google-services.json" />" inside your android .csproj file.

CodePudding user response:

Step 1: Make sure to set reset token = true in MainAcvtivit.cs for your Android-project. This is how I have done it.

//If debug you should reset the token each time.
#if DEBUG
            FirebasePushNotificationManager.Initialize(this, true);
#else
            FirebasePushNotificationManager.Initialize(this, false);
#endif

this your OnTokenRefresh should be triggered.

CrossFirebasePushNotification.Current.OnTokenRefresh  = (s, p) =>
            {
                Debug.WriteLine($"REC TOKEN : {p.Token}");
            };

Step 2: Dubble check that your "build acion" for "google-services.json" is set to "GoogleServicesJson"

Step 3: Check that you have allowed these basic permissions for your app.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pusher.pushnotifications" >
<permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:fullBackupContent="@xml/backup_rules">
    <receiver
        android:name="com.pusher.pushnotifications.reporting.FCMMessageReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.pusher.pushnotifications.fcm.EmptyMessagingService">
        <intent-filter android:priority="1">
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <provider
        android:authorities="${applicationId}.pushnotificationsinitprovider"
        android:name="com.pusher.pushnotifications.internal.PushNotificationsInitProvider"
        android:exported="false"
        android:initOrder="99" />

    <activity android:name="com.pusher.pushnotifications.reporting.OpenNotificationActivity">
        <intent-filter>
            <action android:name="com.pusher.pushnotifications.OPEN_TRACKING" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>
  • Related