c# - 没有互联网的移动通知(仅限本地网络)

标签 c# xamarin xamarin.forms push-notification xamarin.android

我是 xamarin 的新手,我需要从本地服务器发送和接收通知。

我的项目要求没有数据传出大楼。 我们现在有一个移动应用程序可以处理建筑物中的多种类型的事件。 我们希望将通知从服务器发送到移动应用程序。

有人可以建议我阅读一些关于如何实现这一目标的文章吗?

请注意,服务器可以访问互联网,但不能访问为商业网络应用程序和移动应用程序提供服务的路由器。

我发现的一切都与云相关。也许我没有使用权利关键字。

谢谢! :)

编辑: 该大楼有一个没有互联网的 wifi 来为实习生网络应用程序提供服务。 我们需要能够仅在此 wifi 上接收通知。由于覆盖距离,我们无法使用蓝牙。

最佳答案

由于“真正的”推送通知需要向 Google Cloud Services 或 Apple Push Notification Services 注册,因此您必须使用本地通知。

这些是从您的应用程序创建的(下面的示例代码)。但是,您的网络中需要某种服务来提供通知。

我在这里看到两种可能的解决方案:

  1. 您的应用会定期连接到本地服务器并询问是否有任何待处理的通知,如果有则抛出本地通知
  2. 您通过 UDP 连接发送通知,同时您的应用在开放端口上监听(这将允许您将通知直接发送到设备 ip 或使用广播发送到所有设备)

很遗憾,目前我没有任何示例代码,但我想您应该找到大量关于如何调用 api 或如何监听套接字的示例。

但正如所 promise 的,这里是从您的应用创建本地通知的代码。

要创建本地通知,请在您的共享代码中创建一个接口(interface)(例如 ILocalNotification.cs)

public interface ILocalNotification
{
    ShowNotification(string title, string message);
}

然后在您的 native 实现中,添加以下代码:

安卓

public class LocalNotification : ILocalNotification
{
    public void ShowNotification(string title, string message)
    {
        Context act = ((Activity)MainApplication.FormsContext);
        Intent intent = new Intent(act, typeof(MainActivity));

        // Create a PendingIntent; we're only using one PendingIntent (ID = 0):
        const int pendingIntentId = 0;
        PendingIntent pendingIntent =
            PendingIntent.GetActivity(act, pendingIntentId, intent, PendingIntentFlags.OneShot);

        Notification.Builder builder = new Notification.Builder(act)
            .SetContentIntent(pendingIntent)
        .SetContentTitle(title)
        .SetContentText(message)
        .SetSmallIcon(Resource.Drawable.icon);

        Notification notification = builder.Build();
        NotificationManager notificationManager =
            act.GetSystemService(Context.NotificationService) as NotificationManager;
        const int notificationId = 0;
        notificationManager.Notify(notificationId, notification);
    }
}

iOS

public class LocalNotification : ILocalNotification
{
    public void ShowNotification(string title, string message)
    {
        UILocalNotification notification = new UILocalNotification();
        notification.AlertAction = title;
        notification.AlertBody = message;
        notification.ApplicationIconBadgeNumber = 1;
        UIApplication.SharedApplication.ScheduleLocalNotification(notification);
    }
}

然后使用 DependencyService 在共享代码中调用 SendNotification 方法。

关于c# - 没有互联网的移动通知(仅限本地网络),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50979976/

相关文章:

c# - 实体组件系统 c#

c# - 使用 xamarin 在默认锁定屏幕上的 Android Activity

android - Visual Studio Mac,已签名的 Android 版本早于当前版本

c# - 在按钮上向父项添加子项单击 Xamarin.forms

c# - 从托管 C++/CLI 线程调用静态 C# 方法

c# - 如何洗牌多个相关数组?

c# - Xamarin.Forms 动态按钮样式

c# - 无法确定 Xamarin Forms 应用程序是在模拟器中运行还是在设备上运行

Xamarin Forms 在 iOS 上隐藏按钮并在 Android 上显示它

c# - Windows 服务总线检查性能计数器