问题描述
在从本机Android和iOS迁移到Xamarin.Forms的过程中,我决定使用AppCenter Push进行通知,因为它既免费又易于使用(当然,自从我花了很多时间使它能够正常工作这是相对较新的事物,在线指导较少。您可以在How to implement AppCenter Push API?上找到我的原始共享。
我对它感到满意,直到微软宣布它将淘汰AppCenter Push(https://devblogs.microsoft.com/appcenter/app-center-mbaas-retirement/)并鼓励用户转移到Azure(这是一项付费服务)之前。我决定返回使用本地FCM和APN进行推送通知。
问题是没有关于如何完成整个操作的直接教程。在这里和那里都有问题和解决方案,例如iOS .P8仅在我的项目在不支持的.Net Framework上运行时只能在HTTP / 2上运行。只有.Net Core可以使用HTTP / 2协议。
解决方法
我当前的项目在ASP.NET C#上作为后端运行,该后端使用Xamarin.Forms将通知发送到Xamarin.Android和Xamarin.iOS。如果您像我一样,请在下面找到我的答案,我在下面共享可正常使用的C#后端和Xamarin.Forms解决方案。这样一来,更多的用户就可以从免费服务中受益,而不是被迫使用Azure付费服务。
PART 1 C#后端-C#ASP.NET后端。对于FCM和APN,它将分为2部分。
1.1)Firebase(FCM)
-
要设置FCM,您需要注册一个帐户。在线有大量指南,这是很好的https://xmonkeys360.com/2019/12/08/xamarin-forms-fcm-setup-configuration-part-i/之一。请记住获取服务器密钥,并将 google-services.json 文件下载到您的Xamarin.Android项目中。右键单击并将构建操作设置为“ GoogleServiceJson ”(Where can i add google-services.json in xamarin app)。
-
下面是我的Firebase
using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Web.Script.Serialization; namespace PushNotificationLibrary { public class FirebaseCloudMessagingPush { private const string WEB_ADDRESS = "https://fcm.googleapis.com/fcm/send"; private const string SENDER_ID = "YOUR SENDER ID"; private const string SERVER_KEY = "YOUR SERVER KEY"; public string SendNotification(string deviceToken,string title,string message,string priority = "high",int badge = 0,List<Tuple<string,string>> parameters = null) { var result = "-1"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(WEB_ADDRESS); parameters = parameters ?? new List<Tuple<string,string>>(); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add(string.Format("Authorization: key={0}",SERVER_KEY)); httpWebRequest.Headers.Add(string.Format("Sender: id={0}",SENDER_ID)); httpWebRequest.Method = "POST"; if (title.Length > 100) title = title.Substring(0,95) + "..."; //Message cannot exceed 100 if (message.Length > 100) message = message.Substring(0,95) + "..."; JObject jObject = new JObject(); jObject.Add("to",deviceToken); jObject.Add("priority",priority); jObject.Add("content_available",true); JObject jObjNotification = new JObject(); jObjNotification.Add("body",message); jObjNotification.Add("title",title); jObject.Add("notification",jObjNotification); JObject jObjData = new JObject(); jObjData.Add("badge",badge); jObjData.Add("body",message); jObjData.Add("title",title); foreach (Tuple<string,string> parameter in parameters) { jObjData.Add(parameter.Item1,parameter.Item2); } jObject.Add("data",jObjData); var serializer = new JavaScriptSerializer(); using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = jObject.ToString(); streamWriter.Write(json); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); } return result; } } }
1.2)iOS(APN)
- 对于APN,有两种处理方式。一种是使用.P12常规方式。另一种方法是使用.P8使用Apple Auth Key。我更喜欢使用.P8,因为 .P12证书每年到期,并且需要每年更新。使用.P8的问题在于,它使用的是HTTP / 2,而.Net Framework不支持该HTTP / 2,但幸运的是,我设法克服了这一点。请参考How to implement apple token based push notifications (using p8 file) in C#?,在该文章中找到我的答案,以了解如何为.Net Framework实施整个过程。 * 如果您已经在使用.Net Core,答案将与我的类似,但是您不必使用自定义WinHTTPHandler。只需普通的HTTPClient即可。
PART 2 Xamarin.Forms -接下来,您必须在Xamarin.Forms项目中支持该通知。
-
对于这部分,这并不是很难。您所要做的只是参考https://github.com/CrossGeeks/PushNotificationPlugin,下载Nuget并按照链接中的说明为Xamarin项目(Forms,Android,iOS)进行设置。
-
我唯一要强调的是如何以及在何处获取设备令牌。最初,我试图通过下面的代码(OnTokenRefresh)获取设备令牌。但是您很快就会注意到,此代码并不总是被调用,我怀疑只有在刷新令牌后才调用它,而不是在每次调试时才调用。为了每次都获得设备令牌,只需在项目中的任何地方调用
CrossPushNotification.Current.Token
。将该设备令牌注册到您的服务器后端。并使用设备令牌通过上面第1部分中的代码发送通知。CrossPushNotification.Current.OnTokenRefresh += (s,p) => { System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}"); };
就是这样!这很容易,但是我花了数周的时间尝试将它们拼凑在一起之前出错。希望它可以节省与他人的宝贵时间。
,服务器:
在服务器端尝试 FirebaseAdmin。这个包非常简单。
https://github.com/firebase/firebase-admin-dotnet
按照以下设置说明进行操作:
https://firebase.google.com/docs/admin/setup#c
对于 Xamarin 应用:
我决定不想使用 CrossGeeks 插件,它非常简单。
Android:
安装相关的 Xamarin.Firebase 包并在继承包 FirebaseMessagingService 的 Android 项目中创建您自己的 Firebase 消息传递类。
[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)
{
//Your code here to register device token on server
}
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>