cordova-plugin-local-notifications icon indicating copy to clipboard operation
cordova-plugin-local-notifications copied to clipboard

Local notifications not working in ios

Open kanak-infoObjects opened this issue 7 years ago • 36 comments

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.

kanak-infoObjects avatar Jan 05 '18 17:01 kanak-infoObjects

facing the same issue +1

arpitdadheech avatar Jan 05 '18 17:01 arpitdadheech

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 avatar Jan 05 '18 18:01 curlybracketsSARL

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

kanak-infoObjects avatar Jan 05 '18 18:01 kanak-infoObjects

Have you added foreground: true?

JoshuaPerk avatar Jan 05 '18 21:01 JoshuaPerk

@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[]'.

kanak-infoObjects avatar Jan 06 '18 07:01 kanak-infoObjects

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

katzer avatar Jan 06 '18 13:01 katzer

@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?

kanak-infoObjects avatar Jan 06 '18 16:01 kanak-infoObjects

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 avatar Jan 07 '18 13:01 katzer

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

kanak-infoObjects avatar Jan 08 '18 07:01 kanak-infoObjects

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

soimthe1 avatar Jan 08 '18 09:01 soimthe1

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.

katzer avatar Jan 09 '18 12:01 katzer

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 :(

soimthe1 avatar Jan 10 '18 04:01 soimthe1

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 avatar Jan 13 '18 11:01 curlybracketsSARL

@curlybracketsSARL is it working fine for IOS as well?

kanak-infoObjects avatar Jan 15 '18 05:01 kanak-infoObjects

@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.

katzer avatar Jan 16 '18 12:01 katzer

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

curlybracketsSARL avatar Jan 26 '18 19:01 curlybracketsSARL

@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 :)

Yogeshjuya1993 avatar Feb 06 '18 11:02 Yogeshjuya1993

// 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' } });

katzer avatar Feb 12 '18 14:02 katzer

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' } ]
});

znegva avatar Feb 13 '18 11:02 znegva

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

katzer avatar Feb 13 '18 11:02 katzer

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

Thats probably a bug and will be fixed.

katzer avatar Feb 15 '18 14:02 katzer

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

fpauk avatar Mar 01 '18 13:03 fpauk

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.

BorntraegerMarc avatar Jun 27 '18 15:06 BorntraegerMarc

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

BorntraegerMarc avatar Jun 27 '18 15:06 BorntraegerMarc

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...

uklawitter avatar Jul 25 '18 14:07 uklawitter

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 :(

haresh14 avatar Jul 26 '18 17:07 haresh14

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

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

uklawitter avatar Jul 28 '18 07:07 uklawitter

That actually worked for me @uklawitter Thanks a lot!

BorntraegerMarc avatar Aug 10 '18 06:08 BorntraegerMarc

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);

kristianmandrup avatar Nov 27 '19 14:11 kristianmandrup

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.

NiravDhrangdharia avatar Jan 02 '20 06:01 NiravDhrangdharia

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

lovelyelfpop avatar Jan 06 '21 10:01 lovelyelfpop

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

manel00 avatar Mar 10 '21 22:03 manel00

Hi i have the same issue in ios 15 local notification does not work in forground.

` private async setupFCM() { this.fcm.onNotification().subscribe((payload) => { console.log("here is ", payload);

  if (payload.wasTapped) {
    console.log("Received in background -> ", JSON.stringify(payload));
    this.router.navigateByUrl("notifications")
  } else {
    console.log("Received in foreground -> ", JSON.stringify(payload));

    this.localNotifications.schedule({
      id: 1,
      text: payload['body'],
      title: payload['title'],
      foreground: true,
      data: { secret: 'key' }
    });

    this.localNotifications.on('click').subscribe(() => {
      this.router.navigateByUrl("notifications")
    })
  }
})

} `

please help me i use local notification version in package.json

("@ionic-native/local-notifications": "^5.36.0") and ("cordova-plugin-local-notification": "^0.9.0-beta.2",)

alitalaee avatar Oct 28 '21 06:10 alitalaee

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

i tried this but gives me error like below image, please help me to fix it thanks

Screen Shot 1400-08-06 at 09 48 49

alitalaee avatar Oct 28 '21 06:10 alitalaee

@alitalaee My solution is to add some code to cordova-plugin-push, not cordova-plugin-local-notifications

lovelyelfpop avatar Oct 28 '21 08:10 lovelyelfpop

@alitalaee My solution is to add some code to cordova-plugin-push, not cordova-plugin-local-notifications

omm yeah... Does it work for ios and android with FCM? how can i use it with FCM can you explain please thanks a lot

alitalaee avatar Nov 02 '21 08:11 alitalaee