簡體   English   中英

用戶登錄后如何在iOS中注冊推送通知?

[英]How can I register for Push Notifications in iOS after the user logs in?

當用戶首次打開我的應用程序時,他們會收到權限請求的垃圾郵件,其中之一是針對“推送通知”的。 我見過一些人在Swift中引用方法的帖子,但在Xamarin中卻沒有。

我的應用程序可以毫無問題地注冊和接收推送通知,但是我希望在用戶成功登錄后發出推送通知請求。這在Xamarin Forms世界中可能嗎?

我了解在Microsoft Docs( https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started#connect-your -app-to-the-notification-hub )(如果不是全部代碼都在AppDelegate.cs中)。 我缺乏對為什么必須在這里的理解,是否有人有任何指示或閱讀材料來解釋為什么在這里?

任何幫助都將不勝感激。

UPDATE

因此,我現在正在慢慢到達那里,我正在使用一個接口從共享代碼中調用我的iOS項目中的UserNotificationCenterDelegate類。

AppDelegate.cs

public override bool FinishedLaunching(UIApplication app, NSDictionary options) {

        // Unsure if this line is required or not
        UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();

        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());
        return base.FinishedLaunching(app, options);
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
        base.RegisteredForRemoteNotifications(application, deviceToken);

        Console.WriteLine("Hello");
    }

UserNotification

    public UserNotificationCenterDelegate() { }

    public void RegisterForPushNotifications() {
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge, (approved, error) => {
            if (approved) {
                InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
            }
        });
    }

    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
        base.WillPresentNotification(center, notification, 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);
    }

因此,通過DependencyService,我可以調用RegisterForPushNotifiactions但是當接受“推送”權限時,應該在AppDelegate.cs中調用RegisterForRemoteNotifcations ,但不能。

我認為您不必從應用程序代表內部請求推送權限。 從您關注的文章中,刪除以下方法中的所有代碼,該方法要求獲得推送通知權限:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)

然后,將該代碼移到您准備好請求推送許可時要在以后調用的其他一些類/函數。 喜歡:

public static void CheckAndAskPushNotificationPermission()
{
    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    { UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
                                                            (granted, error) =>
    {
        if (granted)
            InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
    });
} else if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
    var pushSettings = UIUserNotificationSettings.GetSettingsForTypes (
            UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
            new NSSet ());

    UIApplication.SharedApplication.RegisterUserNotificationSettings (pushSettings);
    UIApplication.SharedApplication.RegisterForRemoteNotifications ();
} else {
    UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
}

}

無論何時何地,無論您准備如何尋求許可:

PushHelperiOS.CheckAndAskPushNotificationPermission();

在應用程序委托中保留以下方法不變,因為您只能在其中重寫它們(根據您關注的博客):

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)

讓我知道您是否有任何困惑。

暫無
暫無

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

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