Before posting the question let me clear that I have tried found the solution everywhere from google but no success
My Ionic project built using ionic 5
Install package through npm i cordova-plugin-fcm-with-dependecy-updated
app.module.ts
import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
@NgModule({
declarations: [...],
entryComponents: [...],
imports: [...],
providers: [
FCM
],
bootstrap: [AppComponent],
})
export class AppModule {}
app.component.ts
import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
constructor(
private platform: Platform,
private fcm: FCM
) {}
initializeApp() {
this.platform.ready().then(() => {
if (this.platform.is('ios')) {
this.fcm.requestPushPermission().then((context) => {
this.fcm.getToken().then((token) => {
localStorage.setItem(Constants.DEVICETOKEN, token);
console.log(token, 'token.');
console.log(this.deviceTypeId, 'devicetype');
});
})
} else {
this.fcm.getToken().then((token) => {
localStorage.setItem(Constants.DEVICETOKEN, token);
console.log(token, 'token.');
console.log(this.deviceTypeId, 'devicetype');
});
}
this.fcm.onNotification().subscribe((data) => {
console.log(data);
if (data.wasTapped) {
console.log('Received in background');
} else {
this.utility.showSuccessToast(data.body);
console.log('Received in foreground');
}
});
this.fcm.clearAllNotifications();
}
Token saved in SQL database
APIs calls
public static NotificationResponseModel SendNotification(NotificationModel notificationModel)
{
NotificationResponseModel response = new NotificationResponseModel();
try {
FcmSettings settings = new FcmSettings()
{
SenderId = Constant.SenderId,
ServerKey = Constant.ServerKey,
};
HttpClient httpClient = new HttpClient();
string authorizationKey = string.Format("keyy={0}", settings.ServerKey);
string deviceToken = notificationModel.DeviceId;
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorizationKey);
httpClient.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
GoogleNotification.DataPayload dataPayload = new GoogleNotification.DataPayload();
dataPayload.Title = notificationModel.Title;
dataPayload.Body = notificationModel.Body;
GoogleNotification notification = new GoogleNotification();
notification.Data = dataPayload;
notification.Notification = dataPayload;
//notification.Data.Id = notificationModel.Id;
//notification.Data.Type = notificationModel.Type;
var fcm = new FcmSender(settings, httpClient);
var fcmSendResponse = fcm.SendAsync(deviceToken, notification);
if (fcmSendResponse.Result.IsSuccess()) {
response.IsSuccess = true;
response.Message = "Notification sent successfully";
return response;
}
else {
response.IsSuccess = false;
response.Message = fcmSendResponse.Result.Failure.ToString();
return response;
}
}
catch (Exception ex)
{
response.IsSuccess = false;
response.Message = "Something went wrong";
return response;
}
}
My code is worked perfect on android but not 100% working on IOS. I spent lots of time to figure it out but no success
Anyone please look my code and suggest me the mistake
Thanks in advance!
