티스토리 뷰

728x90
반응형

Adroid

1. Nuget 설치
Xamarin.GooglePlayServices.Base
Xamarin.Firebase.Messaging
Xamarin.Google.Android.DataTransport.TransportRuntime
Xamarin.Google.Dagger
Xamrin.Azure.NotificationHubs.Android
Xamarin.Andoidx.Annotation

Ios

Entitlements.plist 더블클릭 > 하단 자격에서 Push Notifications 사용 체크!!

  1. 새로운 클래스를 만들고 아래 UserNotificationcenterDelegate 를 작성한다.
    internal class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {

     public UserNotificationCenterDelegate()
     {
     }
     //This will be called when the notification is received when the app is in Foreground
    
     public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
     {
         // var RequestCode = notification.Request.Content.UserInfo["Code"].ToString();                   
         completionHandler(UNNotificationPresentationOptions.Alert);
         Console.WriteLine("user WillPresentNotification");
     }
    
     public override void DidReceiveNotificationResponse(UNUserNotificationCenter center,             UNNotificationResponse response, Action completionHandler)
     {
         if (response.IsDefaultAction)
         {
             // var Notify = response.Notification.Request.Content;
             // var Code = Notify.UserInfo["Code"];
    
         }
         // Complete handling the notification.
         completionHandler();
        Console.WriteLine("user Noti delegate");
    }
}  
  1. Appdelegate.cs에서

    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
    {
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {

        LoadApplication(new App());
    
       RegisterForRemoteNotifications();
        Firebase.Core.App.Configure();

        UIApplication.SharedApplication.RegisterForRemoteNotifications();


        if (UNUserNotificationCenter.Current != null)
        {
            UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
        }

        Messaging.SharedInstance.Delegate = this;


        return base.FinishedLaunching(app, options);
    }

}

    private void RegisterForRemoteNotifications()
    {
        // Register your app for remote notifications.
        if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
        {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.Current.Delegate = this;
            var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
            UNUserNotificationCenter.Current.RequestAuthorization(authOptions, async (granted, error) =>
            {
                Console.WriteLine(granted);
                await System.Threading.Tasks.Task.Delay(500);
                //await AcessPermissionsAsync();
            });
        }
        else
        {
            // iOS 9 or before
            var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
            var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
        }

        UIApplication.SharedApplication.RegisterForRemoteNotifications();
      //  Messaging.SharedInstance.ShouldEstablishDirectChannel = true;

        Console.WriteLine("here1");
    }


    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        Messaging.SharedInstance.ApnsToken = deviceToken;
        //base.RegisteredForRemoteNotifications(application, deviceToken);
        Console.WriteLine("here2");
    }
    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        // base.FailedToRegisterForRemoteNotifications(application, error);
        Console.WriteLine("here3");
    }
    //public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    //{
    //    completionHandler(UIBackgroundFetchResult.NewData);
    //    Console.WriteLine("here4");
    //}
    [Export("messaging:didReceiveRegistrationToken:")]
    public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
    {
        // Messaging.SharedInstance.FcmToken.UpdateDeviceId();
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
      //  Console.WriteLine("here5:" + fcmToken);

        try
        {
            if (DeviceInfo.DeviceType != DeviceType.Virtual)
            {
                string firebase_text = SecureStorage.GetAsync("FcmToken").Result;
                if (firebase_text != null)
                {
                    SecureStorage.Remove("FcmToken");
                }

                SecureStorage.SetAsync("FcmToken", fcmToken.ToString());
            }

        }
        catch (Exception ex)
        {

            Console.WriteLine("//////////////:" + ex.Message);
        }


    }
    //AppDelegate.cs

    [Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        Console.WriteLine("here4");
      //  ProcessNotification(userInfo, application.ApplicationState == UIApplicationState.Active);

    }

}

2021.04.04 ios push 잘 들어온다.
(정말 온갖 자료 미친듯이 찾아다녔다.ㅜㅜ)

*** 여기참고>> https://techartly.com/fcm-push-notification-ios/
https://github.com/MicrosoftDocs/azure-docs/issues/58976
https://forums.xamarin.com/discussion/184475/xamarin-forms-ios-not-showing-fcm-push-notification-on-ios-physical-device
https://medium.com/@acanales199/this-worked-for-me-with-version-4-3-0-firebase-cloudmessaging-a1def91263b7
https://forums.xamarin.com/discussion/159477/xamarin-forms-push-notification-is-not-receiving-to-ios-device
https://github.com/firebase/firebase-ios-sdk/issues/4996

참고자료.

UNNotificationPresentationOptions.Alert);;)

Android 개발자 활성화

1. 설정> 휴대전화 정보 > 소프트웨어 정보 > 빌드번호 7회 클릭
2. 개발자 옵션 USB디버깅 허용

<참고>

http client 기술자료

http://web.archive.org/web/20180116003143/http://chimera.labs.oreilly.com:80/books/1234000001708/ch14.html

반응형
댓글