none
Firebase Cloud Messaging with Xamarin.Android

    Question

  • I'm new to Xamarin and trying to send push notifications to my app from Firebase. I set up a mobile app on the Firebase console and gave it the same package name as my visual studio project, "RoomCheck.RoomCheck". I downloaded the google-services.json file and followed the "Remote Notifications with Firebase Cloud Messaging" tutorial from Xamarin to set up the app to receive notifications.

     Here is the code I added to the android  manifest:

    <application android:label="RoomCheck" android:theme="@style/MyTheme">
    		<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 added this class to the project, as per the tutorial:

    using System;
    using Android.App;
    using Firebase.Iid;
    using Android.Util;
    
    namespace RoomCheck
    {
        [Service]
        [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
        public class MyFirebaseIIDService : FirebaseInstanceIdService
        {
            const string TAG = "MyFirebaseIIDService";
            public override void OnTokenRefresh()
            {
                var refreshedToken = FirebaseInstanceId.Instance.Token;
                Log.Debug(TAG, "Refreshed token: " + refreshedToken);
                SendRegistrationToServer(refreshedToken);
            }
            void SendRegistrationToServer(string token)
            {
                // Add custom implementation, as needed.        }
        }
    }

    An in the main activity, in the OnCreate method, I tried:

    FirebaseApp.InitializeApp(this);
                Log.Debug(TAG, "InstanceID token: " + FirebaseInstanceId.Instance.Token);

    Google play services is definitely available,  I tried the method in that tutorial to check. However, when I try to get the instance token in my main activity, I get an error on the last line above stating "Default FirebaseApp is not initialized in this process RoomCheck.RoomCheck. Make sure to call FirebaseApp.initializeApp(Context) first." This is despite the fact that I have already called FirebaseApp.InitializeApp.

    Is there another step in the setup that I have missed out on? Or is it a problem with the package name, such as it has to be in a certain format?

    Saturday, April 29, 2017 3:19 PM

All replies

  • Hi katiemorris,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about the development of Xamarin. Since our forum is to discuss the VS IDE, please redirect this appropriate forum: https://forums.xamarin.com/ and start a new thread for your issue. You will get a more professional support from there, thank you for your understanding.

    If you have any other issues about VS IDE in the future, please feel free to let me know.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 01, 2017 5:28 AM