服务器:
尝试使用FirebaseAdmin进行服务器端编程。使用此软件包非常简单。
https://github.com/firebase/firebase-admin-dotnet
请按照以下设置说明进行操作:
:
https://firebase.google.com/docs/admin/setup#c
针对Xamarin应用程序:
我决定不使用CrossGeeks插件,这很简单明了。
针对Android:
安装相关的Xamarin.Firebase包,并在Android项目中创建自己的Firebase Messaging类,继承包FirebaseMessagingService。
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
class PushNotificationFirebaseMessagingService : FirebaseMessagingService
{
private static string foregroundChannelId = "9001";
public override void OnNewToken(string refreshedToken)
{
base.OnNewToken(refreshedToken);
SendRegistrationToServer(refreshedToken);
}
private void SendRegistrationToServer(string token)
{
}
public override void OnMessageReceived(RemoteMessage message)
{
SendNotification(message);
base.OnMessageReceived(message);
}
private void SendNotification(RemoteMessage message)
{
try
{
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
var notificationChannel = new NotificationChannel(foregroundChannelId, "messaging_channel", NotificationImportance.High);
var audioAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
var notificationUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
notificationChannel.EnableLights(true);
notificationChannel.EnableVibration(true);
notificationChannel.SetSound(notificationUri, audioAttributes);
notificationManager.CreateNotificationChannel(notificationChannel);
var remoteNotification = message.GetNotification();
var builder = new Notification.Builder(this, foregroundChannelId)
.SetContentTitle(remoteNotification.Title)
.SetContentText(remoteNotification.Body)
.SetSmallIcon(Resource.Mipmap.icon);
var notification = builder.Build();
notificationManager.Notify(0, notification);
}
catch (Exception ex)
{
}
}
}
请在Application标签中的AndroidManifest.xml文件中添加以下内容。
<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>