IONIC push notification not triggered when in foreground iOS

Hi,
Cometchat message notification doesn’t trigger when the app is in foreground. So when the user is on the app, and a new message comes, the app doesn’t throw a notification.

if (notification.additionalData.foreground == true) {

          // Schedule a single notification

          this.localNotifications.schedule({

            id: 1,

            text: 'Notification from word',

            data: { secret: "key" },

            foreground: true,

          });

I am using - import { Push, PushObject, PushOptions } from ‘@ionic-native/push’; for push notifications.
This happens only in iOS. Not on android!

Hello any update on this? Need this urgently!

Hello @neeraj,

The Push Notifications are working in foreground, background and even in the killed state for Android and iOS both. The problem here is that localNotifications.schedule is not getting called in the foreground (only in iOS) for some reason and that is why it is not showing the notification.

If you are getting the data in the Javascript callback, it means the CometChat Push Notification Extension is working fine.

Also, after spending some time going through LocalNotifications GitHub Issues I found an open issue of local notification not working in iOS when the app is in the foreground. I am sharing the link with you so that you can post your query there and be updated on it.

Local Notification GitHub Issue: https://github.com/katzer/cordova-plugin-local-notifications/issues/1711

Hope this helps

1 Like

Can you tell me how is this working in IONIC?
For ionic I am using the following push plugin to receive notifications from FCM -
For other notifications sent to me from my app server trigger this on Android and iOS.
For iOS, we send 1 additional parameter has - content_available : true in the payload, then it detects
foreground notification as well. So I am assuming cometchat needs to have this parameter too.
Because chat notifications work fine in android - foreground and background.
on iOS, background works fine, foreground doesn’t trigger. For that, content_available: true is needed.

this.pushObject.on('notification').subscribe((notification: any) => {

        // console.log('Received a notification', JSON.stringify(notification));
       The cometchat notification does not trigger "recvd a notification" log.

        alert("Received a notification"+JSON.stringify(notification));


        if (notification.additionalData.foreground == true) {

          // Schedule a single notification

          this.localNotifications.schedule({

            id: 1,

            title:notification.title, 

            text: notification.message,

            data: { secret: "key" },

            foreground: true,

          });

        } else if (notification.additionalData.foreground == false) {

          if (notification.additionalData.type == "notification") {

            this.navCtrl.push("AlertsPage");

          } else {

            // this.navCtrl.push("InboxPage");

          }

        }

      });

Hello @neeraj,

We did a test on our ionic app. We are using FCM plugin of Ionic for Push Notification and everything seems to be working as expected.

However, after going through a few GitHub issues of Push Plugin. I found out an open issue on their GitHub. I am sharing a link to the issue below. Kindly follow this issue for updates.

Also, content_available has nothing to do with foreground push notifications.

FCM Documentation on content-available parameter:

content_available: On iOS, use this field to represent content-available in the APNs payload. When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server. Note that silent notifications in APNs are not guaranteed to be delivered, and can depend on factors such as the user turning on Low Power Mode, force quitting the app, etc.

iOS Documentation on content-available parameter:

To support a background update notification, make sure that the payload’s aps dictionary includes the content-available key with a value of 1

phonegap-push-plugin on content-available parameter:

First the JSON you send from APNS will need to include "content-available": 1 to the aps object. The "content-available": 1 property in your push message is a signal to iOS to wake up your app and give it up to 30 seconds of background processing

You can read about this parameter on the FCM documentation, iOS documentation and also on phonegap-push-plugin documentation.

FCM Documentation: https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json

iOS Documentation: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

phonegap-push-plugin: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#background-notifications-1

I hope this helps!

1 Like

Hello @neeraj,

Can you please remove the Local Notification plugin you are using in your app?

We did test the push notification at our end with FCM plugin and Local Notification plugin in iOS, the onNotification() was not getting triggered. As soon as I removed the Local Notification plugin everything worked as expected.

Hope this helps.

I am actually using push plugin in my ionic app. So the chat notification does not trigger when the iOS app is in foreground. I can remove local notification, but even after that, the code should trigger this -
this.pushObject.on(‘notification’).subscribe((notification: any) when a foreground cometchat notification is received.
So can you recommend a solution for the push plugin? This works fine for android - foreground and background, and for ios - background works fine. Only issue is with foreground.

Hello @neeraj,

Please run your app after removing the Local Notification Cordova plugin. I am sure that this.pushObject.on(‘notification’) will be triggered in the foreground as well as in the background for both iOS and Android. The Local Notification plugin does cause an issue with all the Push Notification ionic plugin. You can check the issues section of phonegap push plugin’s GitHub repository.

Hope this helps

Hello, I tried removing local notification plugin, still the this.pushObject.on(‘notification’) did not get triggered. This issue is happening only on iOS foreground case. Otherwise it seems to be working fine.
For our app notifications, we had solved this by adding “content_available”:true and “priority”:high in the payload.
Our sample payload that works fine for iOS - background as well as foreground -

{
 "to" : "FCM_TOKEN",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
 },
  "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title"
 },
 "priority":"high",
 "content_available":true
} 

Can this be added in cometchat api? This will solve the issue.