使用Xamarin.Android的Firebase云消息传递 [英] Firebase Cloud Messaging with Xamarin.Android

查看:161
本文介绍了使用Xamarin.Android的Firebase云消息传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Xamarin的新用户,并尝试从Firebase向我的应用发送推送通知。我在Firebase控制台上设置了一个移动应用程序,并为其提供了与我的visual studio项目相同的软件包名称,"RoomCheck.RoomCheck"。我下载了google-services.json
文件,然后点击了"使用Firebase云消息传递进行远程通知"。来自Xamarin的教程设置应用程序以接收通知。

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.

 这是我添加到android 清单中的代码:

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

在主要活动中,在OnCreate方法中,我尝试过:

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

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

Google Play服务肯定是可用的,我尝试了该教程中的方法来检查。但是,当我尝试在我的主要活动中获取实例令牌时,我在上面的最后一行收到错误,指出"在此过程RoomCheck.RoomCheck中,默认FirebaseApp未初始化
。请务必先调用FirebaseApp.initializeApp(Context)。尽管事实上我已经调用了FirebaseApp.InitializeApp。

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?

推荐答案

嗨katiemorris,

Hi katiemorris,

欢迎来到MSDN论坛。

Welcome to the MSDN forum.

请参阅您的描述,您的问题是关于Xamarin的开发。由于我们的论坛是讨论VS IDE,请重定向此适当的论坛:
https://forums.xamarin.com/并为您的问题开始一个新线程。您将从那里获得更专业的支持,感谢您的理解。

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.

如果您将来有任何关于VS IDE的问题,请随时告诉我。

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

祝你好运,

Sara


这篇关于使用Xamarin.Android的Firebase云消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆