katzer / cordova-plugin-local-notifications

Cordova Local-Notification Plugin
Apache License 2.0
2.56k stars 1.75k forks source link

Local notifications not working in ios #1492

Open kanak-infoObjects opened 6 years ago

kanak-infoObjects commented 6 years ago

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

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.

arpitdadheech commented 6 years ago

facing the same issue +1

curlybracketsSARL commented 6 years ago

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.

kanak-infoObjects commented 6 years ago

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

JoshuaPerk commented 6 years ago

Have you added foreground: true?

kanak-infoObjects commented 6 years ago

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

katzer commented 6 years ago

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

kanak-infoObjects commented 6 years ago

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

katzer commented 6 years ago

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.

kanak-infoObjects commented 6 years ago

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

soimthe1 commented 6 years ago

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

katzer commented 6 years ago

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.

soimthe1 commented 6 years ago

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

curlybracketsSARL commented 6 years ago

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.

kanak-infoObjects commented 6 years ago

@curlybracketsSARL is it working fine for IOS as well?

katzer commented 6 years ago

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

curlybracketsSARL commented 6 years ago

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

Yogeshjuya1993 commented 6 years ago

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

katzer commented 6 years ago
// 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' } });
znegva commented 6 years ago

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' } ]
});
katzer commented 6 years ago

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

katzer commented 6 years ago

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

Thats probably a bug and will be fixed.

fpauk commented 6 years ago

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

BorntraegerMarc commented 6 years ago

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 commented 6 years ago

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

uklawitter commented 6 years ago

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

haresh14 commented 6 years ago

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

uklawitter commented 6 years ago

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

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

That actually worked for me @uklawitter Thanks a lot!

kristianmandrup commented 4 years ago

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);
NiravDhrangdharia commented 4 years ago

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.

lovelyelfpop commented 3 years ago

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

manel00 commented 3 years ago

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

alitalaee commented 2 years ago

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 commented 2 years ago

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
lovelyelfpop commented 2 years ago

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

alitalaee commented 2 years ago

@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