Hi,
I have integrated the firebase cloudmessaging in my app and i am getting the FCM_token, but not receiving the notifications from firebase console.
I have the packages-------------- xamarine.firebase.ios.cloudmessaging(3.1.2), xamarine.firebase.ios.InstanceID(3.2.1).
Also i have tired the latest packages ,but it keep crashing the app.
In firebasse console i have added the APNs key generated from the appledeveloper account.
Here is my appdelegates.cs
using System;
using System.Collections.Generic;
using System.Linq;
using CarouselView.FormsPlugin.iOS;
using Firebase.CloudMessaging;
using Foundation;
using Microsoft.Identity.Client;
using MSTnTAPP.Helpers;
using MSTnTAPP.Services;
using MSTnTAPP.Util;
using UIKit;
using UserNotifications;
using Xamarin.Forms;
namespace MSTnTAPP.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate,
IUNUserNotificationCenterDelegate,
IMessagingDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
debugAlert("", "FinishedLaunching-called");
global::Xamarin.Forms.Forms.Init();
FormsControls.Touch.Main.Init();
App thisApp = null;
thisApp = new App();
LoadApplication(thisApp);
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// iOS 10 or later
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
{
System.Console.WriteLine(granted);
});
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
// For iOS 10 data message (sent via FCM)
Messaging.SharedInstance.Delegate = this;
}
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();
Firebase.Core.App.Configure();
Messaging.SharedInstance.Delegate = this;
var token = Messaging.SharedInstance.FcmToken ?? "";
NotificationServiceContext.DEVICE_INSTANCE_ID_TOKEN = token;
Console.WriteLine($"FCM token: {token}");
//to get document directory
AppSettingKeys.STORAGE_PATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var authenticationService = DependencyService.Get<IAuthHelper>();
authenticationService.SetParent(this);
CarouselViewRenderer.Init();
return base.FinishedLaunching(app, options);
}
[Export("messaging:didReceiveRegistrationToken:")]
public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
{
Console.WriteLine($"Firebase registration token: {fcmToken}");
debugAlert("", "DidReceiveRegistrationToken-called");
NotificationServiceContext.DEVICE_INSTANCE_ID_TOKEN = fcmToken;
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
debugAlert("", "ReceivedRemoteNotification-called");
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);
// Print full message.
Console.WriteLine(userInfo);
}
/*private void connectFCM()
{
Messaging.SharedInstance.Connect((error) =>
{
if (error == null)
{
Messaging.SharedInstance.Subscribe("/topics/all");
}
System.Diagnostics.Debug.WriteLine(error != null ? "error occured" : "connect success");
});
}*/
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url);
return true;
}
public override void OnActivated(UIApplication uiApplication)
{
debugAlert("", "OnActivated-called");
//connectFCM();
base.OnActivated(uiApplication);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
debugAlert("", "RegisteredForRemoteNotifications-called");
if DEBUG
Firebase.CloudMessaging.Messaging.SharedInstance.SetApnsToken(deviceToken,ApnsTokenType.Sandbox);
endif
if RELEASE
Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken(deviceToken, ApnsTokenType.Prod);
endif
}
public void DidRefreshRegistrationToken(Messaging messaging, string fcmToken)
{
//throw new NotImplementedException();
debugAlert("", $"DidRefreshRegistrationToken-called{fcmToken}");
NotificationServiceContext.DEVICE_INSTANCE_ID_TOKEN = fcmToken;
}
// public override DidReceiveRemoteNotification(Messaging messaging, string fcmToken)
//{
// throw new NotImplementedException();
//}
// iOS 9 <=, fire when recieve notification foreground
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);
//debugAlert("", "DidReceiveRemoteNotification-called");
// Print full message.
Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
{
debugAlert("", "ApplicationReceivedRemoteMessage-called");
Console.WriteLine(remoteMessage);
}
private void debugAlert(string title, string message)
{
var alert = new UIAlertView(title ?? "Title", message ?? "Message", null, "Cancel", "OK");
alert.Show();
}
}
}
I am getting stuck here,please let me know if anything i am missing.