Xamarin iOS 本地推送通知 [英] Xamarin iOS Local Push Notifications

查看:66
本文介绍了Xamarin iOS 本地推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何安排本地(无服务器)推送通知(不是警报)从我的应用程序触发?我只是想从我的应用程序中安排一个通知,并在通知中心的给定时间触发它.我曾尝试使用 LocalNotifications,但它们似乎只有在应用程序打开时才有效,并且只有在应用程序关闭时才更新徽章.除非您使用服务器,否则我也无法发送推送通知.

How can I schedule a local (no server) PUSH notification (not an alert) to fire from my app? I simply want to schedule a notification from my app and have it fire at the given time in the notification center. I have tried to use LocalNotifications but they only seem to work if the app is open, and only update the badge if it is closed. I also see no way of sending push notifications unless you use a server.

目前我可以安排 LocalNotification 并弹出警报,但我希望在应用程序关闭时它可以工作,而不是警报,我想要一个在顶部弹出的推送通知.

Currently I can schedule a LocalNotification and have an alert pop up, but I would like this to work when the app is closed, and instead of an alert, I would like a push notification that pops up at the top.

推荐答案

应在应用启动后立即请求通知权限,方法是将以下代码添加到 AppDelegate 的 FinishedLaunching 方法中 并设置所需的通知类型 (UNAuthorizationOptions):

Notification permission should be requested as soon as the app launches by adding the following code to the FinishedLaunching method of the AppDelegate and setting the desired notification type (UNAuthorizationOptions):

...
using UserNotifications;
...



 public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
   {
       ....

        //after iOS 10
        if(UIDevice.CurrentDevice.CheckSystemVersion(10,0))
        {
            UNUserNotificationCenter center = UNUserNotificationCenter.Current;

            center.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.UNAuthorizationOptions.Badge, (bool arg1, NSError arg2) =>
                 {

                 });

            center.Delegate = new NotificationDelegate();
        }

        else if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {

            var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert| UIUserNotificationType.Badge| UIUserNotificationType.Sound,new NSSet());

            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);

        }

        return true;
    }

iOS 10 的新功能,当应用程序在前台并触发通知时,它可以以不同的方式处理通知.通过提供UNUserNotificationCenterDelegate 并实现UserNotificationCenter 方法,应用可以接管显示通知的责任.例如:

New to iOS 10, an app can handle Notifications differently when it is in the foreground and a Notification is triggered. By providing aUNUserNotificationCenterDelegate and implementing theUserNotificationCentermethod, the app can take over responsibility for displaying the Notification. For example:

using System;
using ObjCRuntime;
using UserNotifications;


namespace workplat
{
 public class NotificationDelegate:UNUserNotificationCenterDelegate
   {
    public NotificationDelegate()
    {
    }

    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Do something with the notification
        Console.WriteLine("Active Notification: {0}", notification);

        // Tell system to display the notification anyway or use
        // `None` to say we have handled the display locally.
        completionHandler(UNNotificationPresentationOptions.Alert|UNNotificationPresentationOptions.Sound);
    }


    public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        // Take action based on Action ID
        switch (response.ActionIdentifier)
        {
            case "reply":
                // Do something
                break;
            default:
                // Take action based on identifier
                if (response.IsDefaultAction)
                {
                    // Handle default action...
                }
                else if (response.IsDismissAction)
                {
                    // Handle dismiss action
                }
                break;
        }

        // Inform caller it has been handled
        completionHandler();
    }

  }
}

要在系统中创建和注册自定义操作,请使用以下代码:

To create and register a Custom Action with the system, use the following code:

 public void RegisterNotification(long time)
    {
        UNUserNotificationCenter center = UNUserNotificationCenter.Current;

        //creat a UNMutableNotificationContent which contains your notification content
        UNMutableNotificationContent notificationContent = new UNMutableNotificationContent();

        notificationContent.Title = "xxx";
        notificationContent.Body= "xxxx";

        notificationContent.Sound = UNNotificationSound.Default;

        UNTimeIntervalNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(time, false);

        UNNotificationRequest request = UNNotificationRequest.FromIdentifier("FiveSecond", notificationContent, trigger);


        center.AddNotificationRequest(request,(NSError obj) => 
        {



        });

    }

当你调用这个方法时,例如:

When you call this method ,for emample:

RegisterNotification(20);//set the time you want to push notification

通知将在 20 秒后推送,如果您关闭应用程序.

The notification will been pushed after 20 seconds,enen if you close your app.

我的demo已经上传到我的github,大家可以下载参考:演示链接.

I have upload my demo to my github, you can download it for your reference: Demo Link .

您可以访问链接以获取更多信息和详细信息:微软文档

And you can access the link for more information and details: MicroSoft Document

这篇关于Xamarin iOS 本地推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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