簡體   English   中英

Xamarin iOS 中的可操作推送通知

[英]Actionable push notification in Xamarin iOS

        var acceptAction = UNNotificationAction.FromIdentifier("AcceptAction", "Accept", UNNotificationActionOptions.None);
            var declineAction = UNNotificationAction.FromIdentifier("DeclineAction", "Decline", UNNotificationActionOptions.None);

                // Create category
            var meetingInviteCategory = UNNotificationCategory.FromIdentifier("MeetingInvitation",
                new UNNotificationAction[] { acceptAction, declineAction }, new string[] { }, UNNotificationCategoryOptions.CustomDismissAction);

            // Register category
            var categories = new UNNotificationCategory[] { meetingInviteCategory };
            UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));

您如何接收自定義的可操作推送通知以及需要將上述代碼放在哪個文件中?

在 iOS 應用程序可以向用戶發送通知之前,該應用程序必須在系統中注冊,並且由於通知是對用戶的干擾,因此應用程序必須在發送之前明確請求許可。

通過將以下代碼添加到AppDelegateFinishedLaunching方法並設置所需的通知類型 (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 方法,應用程序可以接管顯示通知的責任。 例如:

using System;
using ObjCRuntime;
using UserNotifications;


namespace xxx
{
 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();
    }

  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM