question

AlexanderBlohm-3666 avatar image
0 Votes"
AlexanderBlohm-3666 asked WenyanZhang-MSFT commented

Firebase not getting push token on first startup

Hello, do anyone knows why Firebase wont register with a push token the first time you start an app?
Heres my code:

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override void DidEnterBackground(UIApplication application)
{
Globals.ErIBakgrunn = true;
Console.WriteLine("[BG_MODE] (***) App entering background state.");
}

     public override void WillEnterForeground(UIApplication application)
     {
         Globals.ErIBakgrunn = false;
         Console.WriteLine("[BG_MODE] (***) App will enter foreground");
     }

     public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
     {
         // Get current device token
         var DeviceToken = deviceToken.Description;
         //deviceToken.
         if (!string.IsNullOrWhiteSpace(DeviceToken))
         {
             DeviceToken = DeviceToken.Trim('<').Trim('>');
         }

         // Get previous device token
         var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");


         NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
         Globals.FCM_TOKEN = Messaging.SharedInstance.FcmToken ?? "";
                     PKDebugger.ConsoleOutput("[TOKEN] (***) VI har token: " + Messaging.SharedInstance.FcmToken);

   
     }


     //
     // This method is invoked when the application has loaded and is ready to run. In this 
     // method you should instantiate the window, load the UI into it and then make the window
     // visible.
     //
     // You have 17 seconds to return from this method, or iOS will terminate your application.
     //

     public class CarIntegrationBridge
     {
         public static void Init()
         {
            
         }
     }
     public override void OnActivated(UIApplication uiApplication) {
          }

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

            
         global::Xamarin.Forms.Forms.Init();
         MobileAds.SharedInstance.Start(CompletionHandler);


            
         Xamarin.FormsMaps.Init();
         Rg.Plugins.Popup.Popup.Init();
         CarIntegrationBridge.Init();

         List<string> adTestEnheter = new List<string>();

         adTestEnheter.Add("62604a48fb5fb1e00e66202b148ba45b");
         adTestEnheter.Add("GADSimulatorID");

         PKDebugger.ConsoleOutput("(!!!) Device ID: " + UIDevice.CurrentDevice.IdentifierForVendor.ToString());
       //  CrossMTAdmob.Current.TestDevices = myStuffz;
          

         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);
         }

         Firebase.Core.App.Configure();

        
         LoadApplication(new App());


         return base.FinishedLaunching(app, options);
     }

     private void CompletionHandler(InitializationStatus status) { PKDebugger.ConsoleOutput("[ADS] (***) CompletionHandler is running..."); }


     [Export("messaging:didReceiveRegistrationToken:")]
     public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
     {
         PKDebugger.ConsoleOutput($"Firebase registration token: {fcmToken}");
         Globals.FCM_TOKEN = fcmToken;
            
     }
 }
dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You mean that var DeviceToken = deviceToken.Description; is null, right? Do you uninstall the app from your device and install again for testing? Did it trigger FailedToRegisterForRemoteNotifications when you start your app at the first time?


0 Votes 0 ·

0 Answers