0

当应用程序被杀死时,总是会收到通过扩展程序的推送通知,我可以通过扩展程序打印的日志在设备上注意到这一点。当应用程序在前台时,什么都没有发生,我在扩展程序或 AppDelegate 的 WillPresentNotification 方法中都没有看到日志(该方法在前台完美地工作,“正常”推送不需要被扩展程序拦截)。我还尝试添加方法 ReceivedRemoteNotification(...),正如我从日志中看到的那样,这个方法都没有被调用。

我使用 UNNotificationServiceExtension 来映射通知的内容。

[Register("NotificationService")]

public class NotificationService : UNNotificationServiceExtension
{
    Action<UNNotificationContent> ContentHandler { get; set; }
    UNMutableNotificationContent BestAttemptContent { get; set; }

    .
    .
    .

    protected NotificationService(IntPtr handle) : base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }

    public override void DidReceiveNotificationRequest(UNNotificationRequest request,
                                                       Action<UNNotificationContent> contentHandler)
    {
        Console.WriteLine("NOTIFICATIONS: NotificationExtension: entered DidReceive");

        ContentHandler = contentHandler;
        BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();

        // Mapping of body and title

        ContentHandler(BestAttemptContent);
    }


    public override void TimeWillExpire()
    {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.

        Console.WriteLine("NOTIFICATIONS: NotificationExtension: entered TimeWillExpire");

        ContentHandler(BestAttemptContent);
    }
}

在 AppDelegate 我有代码在前台显示通知:

[Register("AppDelegate")]
public partial class AppDelegate : FormsApplicationDelegate,
                                   IUNUserNotificationCenterDelegate,
                                   IUIAlertViewDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary launchOptions)
    {
        LogBroker.Instance.TraceDebug("START");
        
        .
        .
        .
        
        LogBroker.Instance.TraceDebug("RegisterForNotifications: Register For Push Notification");
        UNUserNotificationCenter.Current.RequestAuthorization(
            UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
            (approved, err) => {

                if (err != null)
                {
                    LogBroker.Instance.TraceError($"RegisterForNotifications: {err.LocalizedDescription}");
                    return;
                }

                if (approved)
                {
                    InvokeOnMainThread(() =>
                    {
                        LogBroker.Instance.TraceDebug("RegisterForNotifications: Approved");
                        UIApplication.SharedApplication.RegisterForRemoteNotifications();
                    });
                }
                else
                {
                    LogBroker.Instance.TraceWarning($"RegisterForNotifications: Rejected by user!");
                }

            });

        UNUserNotificationCenter.Current.Delegate = this;

        LoadApplication(new App());
        
        return base.FinishedLaunching(app, launchOptions);
    }

    .
    .
    .

    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter _, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        LogBroker.Instance.TraceDebug($"NOTIFICATIONS: entered WillPresentNotification");

        var pushType = notification.Request.Content.UserInfo?.ValueForKey(new NSString("push-type"))?
                                   .ToString() ?? "";

        LogBroker.Instance.TraceDebug($"NOTIFICATIONS: push type - {pushType}");

        if (pushType == "call")
        {
            completionHandler(UNNotificationPresentationOptions.None);
        }
        else
        {
            LogBroker.Instance.TraceDebug($"NOTIFICATIONS: show push notification");
            completionHandler(UNNotificationPresentationOptions.Alert |
                              UNNotificationPresentationOptions.Sound);
        }
    }
}

通知的有效负载是这样的:

{
aps =     {
    alert =         {
        "loc-args" =             (
            "***"
        );
        "loc-key" = "IM_MSG";
    };
    badge = 1;
    "mutable-content" = 1;
    sound = "msg.caf";
};
"call-id" = 909d32775168c0db;
"domain-name" = "***";
panda = blue;
"pn_ttl" = 30;
"push-services" = vdes;
"push-type" = message;
"text-message" = "DND;ON";

}

4

0 回答 0