I have done the following things:
- Created a project in FCM console and added ios project into it.
- Downloaded the
GoogleService-Info.plist
and added it to the xamarin forms ios project directory and set the build action asBundlesource
. - On Info.plist enabled remote notification background mode.
- Added FirebaseAppDelegateProxyEnabled in the app’s Info.plist file and set it to No.
- Created a provisioning profile and distribution certificate and installed it into the keychain access. Also, mapped these certificates in the project options.
- Uploaded .p12 certificate in FCM console.
- Added codes for handling push notification in
AppDelegate.cs
.
My Code:
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App("",""));
RequestPushPermissionsAsync();
_launchoptions = options;
return base.FinishedLaunching(app, options);
}
NSDictionary _launchoptions;
public override void OnActivated(UIApplication uiApplication)
{
base.OnActivated(uiApplication);
if (_launchoptions != null && _launchoptions.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
{
var notfication = _launchoptions[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
PresentNotification(notfication);
}
_launchoptions = null;
}
async Task RequestPushPermissionsAsync()
{
var requestResult = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(
UNAuthorizationOptions.Alert
| UNAuthorizationOptions.Badge
| UNAuthorizationOptions.Sound);
bool approved = requestResult.Item1;
NSError error = requestResult.Item2;
if (error == null)
{
if (!approved)
{
Console.Write("Permission to receive notification was not granted");
return;
}
var currentSettings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync();
if (currentSettings.AuthorizationStatus != UNAuthorizationStatus.Authorized)
{
Console.WriteLine("Permissions were requested in the past but have been revoked (-Settings app)");
return;
}
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
Console.Write($"Error requesting permissions: {error}.");
}
}
public async override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
if (deviceToken == null)
{
return;
}
Console.WriteLine($"Token received: {deviceToken}");
await SendRegistrationTokenToMainPRoject(deviceToken);
}
async Task SendRegistrationTokenToMainPRoject(NSData deviceToken)
{
MessagingCenter.Send<object, string>(this, "fcmtoken", deviceToken.ToString());
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
Console.WriteLine($"Failed to register for remote notifications: {error.Description}");
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
Action<UIBackgroundFetchResult> completionHandler)
{
PresentNotification(userInfo);
completionHandler(UIBackgroundFetchResult.NoData);
try
{
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
var message = (aps[new NSString("webContentList")] as NSString).ToString();
LoadApplication(new App("", message));
}
catch (Exception ex)
{
//LogInfo.ReportErrorInfo(ex.Message, ex.StackTrace, "AppDelegate-DidReceiveRemoteNotification");
}
}
void PresentNotification(NSDictionary userInfo)
{
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
var msg = string.Empty;
if (aps.ContainsKey(new NSString("alert")))
{
msg = (aps[new NSString("alert")] as NSString).ToString();
}
if (string.IsNullOrEmpty(msg))
{
msg = "(unable to parse)";
}
MessagingCenter.Send<object, string>(this, App.NotificationReceivedKey, msg);
}
}
I am not using any NuGet packages in ios part. When running the project into a device, the FCM token generating part is working and I can see the fcm token on the VS console. I tried to send a test notification to a device from postman but getting InvalidRegistration
error.
Postman Response
{
"multicast_id": 8754155136812875313,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
]
}
Postman Body
{
"to" : "d20ad003 7473bfba 85dffc33 1534decf b4b886f1 c738878f fd7f2c60 d9dabc36",
"collapse_key" : "type_a",
"data" : {
"body" : "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
},
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification",
"sound": "default",
"content_available" : true
}
}
I have completed the android part implementation and notifications are receiving when push from the postman. But getting InvalidRegistration
error on postman and not receiving any notification in my ios device.
Anyone suggest a solution for this issue.
Thanks in advance.