Cordova-plugin-local-notifications: Local notifications not working in ios

Created on 5 Jan 2018  路  32Comments  路  Source: katzer/cordova-plugin-local-notifications

WARNING: IF YOU IGNORE THIS TEMPLATE, WE'LL IGNORE YOUR ISSUE. YOU MUST FILL THIS IN!

Provide a general summary of the issue.

Your Environment

  • Plugin version: 0.8.5
  • Platform: IOS
  • OS version: 11.1.2
  • Device manufacturer / model: Apple
  • Cordova version (cordova -v): 7.1.0
  • Cordova platform version (cordova platform ls): Installed platforms:
    android 6.2.2
    ios 4.5.4
  • Plugin config
  • Ionic Version (if using Ionic) 2.0.0

Expected Behavior

When the app is in foreground, I want a local notification.

Actual Behavior

No notification arrives but as soon as I take my app in the background, the notification comes.

Steps to Reproduce

I'm using cordova-plugin-fcm for push notifications in case of background and app killed. In case of app in foreground, I'm scheduling local notification using the plugin. This is my code:

            FCMPlugin.onNotification((msg) => {
                this.notificationMsg = msg;
                if (msg.wasTapped) {
                 // code for app background and app killed
                }
                else {
                    LocalNotifications.schedule({
                            id: 1,
                            title: 'title',
                            text: 'text',
                        })
               }
            })

But no local notifications comes up in foreground. But as soon as I minimize the app, it comes. What's the issue here? Anything to do with plugin version? Please help.

bug enhancement ios

Most helpful comment

Adding "foreground: true" solved this problem for me:

const notification = { id, title, text, silent: false, foreground: true };
this.notifications.schedule(notification);`

All 32 comments

facing the same issue
+1

I'm pretty sure using FCM plugin with this one here won't work. There are conflicts, I am still looking for a solution but uninstalling FCM makes local notifications works and vice versa.

@curlybracketsSARL so what can I do now?? I need notifications in all 3 cases - foreground, background and app killed.

Have you added foreground: true?

@JoshuaPerk you mean like this:

LocalNotifications.schedule({ id: 1, title: 'msg.title', text: 'msg.text', foreground: true })

This is giving me a error that 'foreground' does not exist in type 'ILocalNotification | ILocalNotification[]'.

foreground requires v0.9-beta. v0.8.x doesn't fully support iOS10+

@katzer i tried with that. Local notification arrived when I tried normally but when the same code was written inside the callback of FCMPlugin, it didn't work. Guess the two plugins aren't compatible?

I havent used the other plugin by myself. Since the code works outside of the callback, you could try to wrap the code inside the callback with setTimeout. Maybe that helps.

@katzer the callback of FCMPlugin never even gets called with the 0.9-beta version of this plugin. what now?

Using Cordova 8.0.0 and latest Local Notification plugin , no notification is fired on ios :( please help.

By myself I have no trouble using latest versions. I have all latest versions installed and running. It might be possible that the FCMPlugin still uses the deprecated API instead of user-notifications API which is a conflict.

I noticed that it has something to do with number of notifications, my requirement is to register nearly 250 notifications all at once, when i tried registering/scheduling 5 or 10 , this works fine and when i register all of them it doesn't work, i also tried to group 50 notifications at a time (using an array) and schedule them in one shot, so only 50x5 i.e. 5 times schedule will be fired, but still no luck.
I did this because i read somewhere on the forums that IOS 11 doesn't allow registering more than 64 notifications at a time.
Badly stuck now :(

I ended up using OneSignal (with Firebase functions consuming their REST API) instead of FCM directly... Both this plugin and OneSignal work great side-by-side and we're having no issues so far.

@curlybracketsSARL is it working fine for IOS as well?

@soimthe1 There's a system limit already before iOS 11. If there's a logic/interval behind each notification its wise to schedule repeating notifications instead of hundreds with a fixed date.

@kanak-infoObjects Yeah, working perfectly. No issues whatsoever (so far).

@katzer can you send me code which code you are using for IOS because it's creating problem
"cordova.plugins.notification.local.launchDetails"

In foreground mode app dose not return notification and if we are in background mode app is returned notification but i can't get information in launchDetails, trigger & onclick.
and when app is kill same problem.

Please Help me
Thank you in advance :)

// With cordova-plugin-local-notification v0.9-beta

var plugin  = cordova.plugins.notification.local,
    details = plugin.launchDetails;

if (details) {
    alert('Launched by notification with ID ' + details.id);
}

plugin.on('trigger', function(toast, e) { console.log(toast); });
plugin.on('click', function(toast, e) { console.log(toast); });

plugin.schedule({ id: 1, text: 'A sample toast', sound: true, trigger: { in: 5, unit: 'second' } });

In fact the normal click on a notification to open/launch the App is working on iOS.

What is not working on iOS is getting launchDetails when the app was launched by clicking on an action - launchDetails is empty/undefined in this case.

I think this is due to this line in src/ios/APPLocalNotification.m - _launchDetails is only set if the action/event is equal to click... not sure why this limitation was introduced in the first place.

To extend the example code above:

// With cordova-plugin-local-notification v0.9-beta.1

var plugin  = cordova.plugins.notification.local,
    details = plugin.launchDetails;

if (details) {
    alert('Launched by notification with ID ' + details.id);
}

plugin.on('trigger', function(toast, e) { console.log(toast); });
plugin.on('click', function(toast, e) { console.log(toast); });
plugin.on('actionclick', function(toast, e) { console.log(toast); });

plugin.schedule({
   id: 1,
   text: 'A sample toast',
   sound: true,
   trigger: { in: 5, unit: 'second' },
   foreground: true,   //this seems to be needed on iOS
   actions: [ { id: 'actionclick', launch: true, title: 'Click me' } ]
});

BTW please note that launchDetails is broken on iOS with beta.2

_launchDetails is only set if the action/event is equal to click

Thats probably a bug and will be fixed.

Build with this in Phonegap works fine in Android
But in iOS 11 on my ipad I Hear the notification sound but it dont show up

I can confirm what @fpauk said. on v0.9-beta3 I hear the sound when the app is in foreground but no notification appears.

Funny thing is this behaviour only happens when I install my iOS app the FIRST time from XCode (after uninstalling it).

When I force-kill the app and open it normally then iOS notifications are appearing when the app is in foreground.

I'm using native VoIP push to trigger local notifications. So no other library is involved.

When I'm using the this.localNotifications.requestPermission() function then the first notification (right after the prompt) is displayed and the following are not.

I kind of have the same problem like @fpauk . I can send notifications and they show up, but when I put the app in background and than in foreground again, the notifications just generate a vibration, but no popup on iOS...

I'm having the same issue as @BorntraegerMarc mentioned, related to only hear sound but no notification displayed.

Please help, I'm stuck and have to solve it ASAP :(

Adding "foreground: true" solved this problem for me:

const notification = { id, title, text, silent: false, foreground: true };
this.notifications.schedule(notification);`

That actually worked for me @uklawitter Thanks a lot!

Can you add custom launch details?

const launchDetails = {
  id: 1,
  text: "A sample toast",
  sound: true,
  trigger: { in: 5, unit: "second" },
  foreground: true,   //this seems to be needed on iOS
  actions: [ { id: 'actionclick', launch: true, title: 'Click me' } ],

  // custom launch details ...
  type: "transfer-event" 
};

plugin.schedule(launchDetails);

Can you please help me with this issue?

I installed local and FCM plugins both in my ionic 3 app.
I am getting FCM push notification and also getting local notification.
I have logic like local notification triggering at every 3hours of interval within day time. I am getting all these local notification except 2nd last local notification.
I have make log file for the triggering date of local notification and its giving all triggered date time properly but only 2nd last local notification is not receiving in device.

e.g.
I have scheduled local notification at time 10AM, 1PM, 4PM, 7PM and 10PM.
Only 7PM time local notification not received in iOS device. All other received at defined time.

cordova-plugin-push is conflict with this plugin, change code below of cordova-plugin-push solved this problem.
change

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    NSLog( @"NotificationCenter Handle push from foreground" );
    // custom code to handle push while app is in the foreground
    PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"];
    pushHandler.notificationMessage = notification.request.content.userInfo;
    pushHandler.isInline = YES;
    [pushHandler notificationReceived];

    completionHandler(UNNotificationPresentationOptionNone);
}

to

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    UNNotificationRequest* toast = notification.request;
    if ([toast.trigger isKindOfClass:UNPushNotificationTrigger.class]) // add this line
    {
        NSLog( @"NotificationCenter Handle push from foreground" );
        // custom code to handle push while app is in the foreground
        PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"];
        pushHandler.notificationMessage = notification.request.content.userInfo;
        pushHandler.isInline = YES;
        [pushHandler notificationReceived];

        completionHandler(UNNotificationPresentationOptionNone);
    }
}

Local notification for ios is working now

Where did you get this code? I dont have it @lovelyelfpop ... Thank you

Was this page helpful?
0 / 5 - 0 ratings