【问题标题】:send push notification from one android app to another when data in sql server database changes当 sql server 数据库中的数据发生变化时,从一个 android 应用程序向另一个应用程序发送推送通知
【发布时间】:2021-08-22 11:33:53
【问题描述】:
【问题讨论】:
标签:
c#
push-notification
xamarin.android
firebase-cloud-messaging
【解决方案1】:
要在 Xamarin.android 中将通知从一台设备发送到另一台设备,有几种方法:
-
向数据库添加新行时使用 Firebase 云消息传递
例子:
//添加新行的过程
//发布在 FCM 上
注意:接收设备必须在 firebase 云消息中注册设备令牌
更多细节:
https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/firebase-cloud-messaging
-
在接收设备的后台使用任务工作并检查数据库中的行数
示例:
Task.Run(()=>{
//检查行数的代码
});
如果改变调用这个方法:
public void SendNotification(int periority_notification, return_user_info User_info, Context context)
{
/*here intent to open layout when click on notification*/
Intent inten = new Intent(context, typeof(MainActivity));
const int pendingIntentId = 0;
PendingIntent pendingIntent =
PendingIntent.GetActivity(context, pendingIntentId, inten, PendingIntentFlags.OneShot);
var title = User_info.User_Email;
var message = User_info.User_Name.Trim() + " Reserved Request";
using (var notificationManager = NotificationManager.FromContext(context))
{
Notification notification;
if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
{
notification = new Notification.Builder(context)
.SetContentIntent(pendingIntent)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetPriority(1)
.SetSmallIcon(Resource.Drawable.carparts)
.SetDefaults(NotificationDefaults.All)
.Build();
}
else
{
var myUrgentChannel = context.PackageName;
const string channelName = "SushiHangover Urgent";
NotificationChannel channel;
channel = notificationManager.GetNotificationChannel(myUrgentChannel);
if (channel == null)
{
channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
channel.EnableVibration(true);
channel.EnableLights(true);
channel.LockscreenVisibility = NotificationVisibility.Public;
notificationManager.CreateNotificationChannel(channel);
}
channel?.Dispose();
notification = new Notification.Builder(context)
.SetContentIntent(pendingIntent)
.SetChannelId(myUrgentChannel)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.carparts)
.Build();
}
notificationManager.Notify(periority_notification, notification);
notification.Dispose();
}
}