katzer / cordova-plugin-local-notifications

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

Not asking for permissions to show alerts (iOS 8) #288

Closed timbergus closed 9 years ago

timbergus commented 9 years ago

I am creating an app that check the position of the user in background and then notify him when reaching a place. The problem is that the alerts never show because they don't have the user's permission to show. How I need to configure the plugin or the project to force my app asking permission to the user?

The messages I have in Xcode are the following:

Attempting to schedule a local notification [...] with an alert but haven't received permission from the user to display alerts
Attempting to schedule a local notification [...] with a sound but haven't received permission from the user to play sounds

Could you please help me?

Thanks in advance,

Gustavo

MichelReij commented 9 years ago

Hi Gustavo, The latest version of the plugin, 0.7.6, has 2 new methods: hasPermission and promptForPermission. These will help you prompt the user for permission.

https://github.com/katzer/cordova-plugin-local-notifications/tree/0.7

timbergus commented 9 years ago

Thanks Michel,

Works great and now after checking the permissions I forced the request and the prompt appears, but after granting permissions, when the local notification is going to be added and launched, I receive this error:

[APPLocalNotification isNotificationScheduledWithId:]: unrecognized selector sent to instance 0x1700938d0
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[APPLocalNotification isNotificationScheduledWithId:]: unrecognized selector sent to instance 0x1700938d0'

I have commented the function where I have the error, and these are the lines where the debugger stops:

add -> if ([self isNotificationScheduledWithId:id]) {
isNotificationScheduledWithId -> UILocalNotification* notification = [self notificationWithId:id];
notificationWithId -> NSString* notId = [[notification.userInfo objectForKey:@"id"] stringValue];

My code is the example code:

window.plugin.notification.local.add({
    message: 'Notifications are working!'
});

It seems to have problems with the idpassed to the functions, but I have tried passing a value to id as number, as string and no value at all, and it does not work.

Maybe I'm doing something wrong, but I have searched in Google and there is no information or examples about this problem. Could you help me again?

Sorry for the inconveniences and thanks in advance,

Gustavo

MichelReij commented 9 years ago

Hi Gustavo, a few of us had that problem too after switching over to iOS 8. 'Orbitaloop' has a working solution in the first message of this thread: https://github.com/katzer/cordova-plugin-local-notifications/issues/281

Cheers, Michel

Grtjs,

Michel


Michel Reij Sport & Portret www.mreij.nl michel.reij@gmail.com Eerste Brandenburgerweg 41 3721 MC Bilthoven 06 - 8173 4302 KvK 52645010

2014-10-12 16:49 GMT+02:00 Gustavo Muñoz notifications@github.com:

Thanks Michel,

Works great and now after checking the permissions I forced the request and the prompt appears, but after granting permissions, when the local notification is going to be added and launched, I receive this error:

[APPLocalNotification isNotificationScheduledWithId:]: unrecognized selector sent to instance 0x1700938d0 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[APPLocalNotification isNotificationScheduledWithId:]: unrecognized selector sent to instance 0x1700938d0'

I have commented the function where I have the error, and these are the lines where the debugger stops:

add -> if ([self isNotificationScheduledWithId:id]) { isNotificationScheduledWithId -> UILocalNotification* notification = [self notificationWithId:id]; notificationWithId -> NSString* notId = [[notification.userInfo objectForKey:@"id"] stringValue];

My code is the example code:

window.plugin.notification.local.add({ message: 'Notifications are working!' });

It seems to have problems with the idpassed to the functions, but I have tried passing a value to id as number, as string and no value at all, and it does not work.

Maybe I'm doing something wrong, but I have searched in Google and there is no information or examples about this problem. Could you help me again?

Sorry for the inconveniences and thanks in advance,

Gustavo

— Reply to this email directly or view it on GitHub https://github.com/katzer/cordova-plugin-local-notifications/issues/288#issuecomment-58802758 .

timbergus commented 9 years ago

Works like a charm!!

Thanks :)

timbergus commented 9 years ago

What katzer told me was to use the 0.7 branch instead master branch. Change the branch in GitHub (top of the page) and try to install with the instructions on this page. It works.

Then, if you have problems with the permissions prompt, check this solution.

https://github.com/katzer/cordova-plugin-local-notifications/issues/281

Best regards

On 13 Oct 2014, at 14:37, Stijn D. notifications@github.com wrote:

Hi I've noticed the same issue's with having no permission etc... i've updated cordova and this plugin but i keep getting the following error:

2014-10-13 14:32:38.173 locnot[974:409095] ERROR: Method 'hasPermission:' not defined in Plugin 'LocalNotification' 2014-10-13 14:32:38.174 locnot[974:409095] -[CDVCommandQueue executePending] [Line 158] FAILED pluginJSON = [ "LocalNotification985119547", "LocalNotification", "hasPermission", [

  ]

] Did I not update the plugin correctly? (I have updated the local-notifications plugin by removing it and then adding it again?) Or is this really an issue?

— Reply to this email directly or view it on GitHub.

markwpearce commented 9 years ago

@timbergus My App doesn't seem to show the Notifications Permission Prompt. I'm calling promptForPermission() in Device ready, but I never get a pop-up on my phone. I do end up getting local notifications, but with no sound (I get the "Attempting to schedule a local notification [...] with a sound but haven't received permission from the user to play sounds" error mentioned above)

What's weird is that it seems to work on the Simulator - meaning the notification popup prompt appears.

Any Ideas?

MichelReij commented 9 years ago

@markwpearce Are you certain you used the 0.7 branch as indicated above, more specifically the 0.7.6 version? That is important, as the Master branch doesn't yet have the iOS 8 permission request method.

szuzul commented 9 years ago

I'm having the same problem, cordova plugins ls list me 0.7.6. version IOS 8.. I also add a hack from https://github.com/katzer/cordova-plugin-local-notifications/issues/281. I take some time to testing and it looks like permission popup shows up first time when you install it on device but after you deinstall it and run again, permission popup doesn't shows up any more. Permission popup should shown every time on new installation.

MichelReij commented 9 years ago

@szuzul: I don't think that the popup should show more than once. That would be an annoyance that Apple would not allow. In my app, I run the check for permission every time the user creates a new notification. If no permission is given, I show the permission request popup the first time. Then I check again, recursively, what the user's response was. If he has refused permission, I show a warning sign explaining that my app is unable to notify him. I refer to our website's support section for an explanation on how to give permission through the Settings app.

    checkPermissions: function() {

        var appData = this;

        if (appData.isIOS() && appData.isPhone() && window.plugin.notification.local.hasPermission) {
            // Check permission for notifications

            window.plugin.notification.local.hasPermission(function(granted) {

                if (granted) {

                    $('#NoRightsWarning').hide();       // Hide the warning message

                } else {

                    // Alas, no permission granted (yet)

                    if (appData.getUser().getHasBeenAsked4Permission()) {

                        // The user has been asked for permission already but refused... :-(
                        $('#NoRightsWarning').show();       // Show the warning message

                    } else {

                        window.plugin.notification.local.promptForPermission();
                        appData.getUser().setHasBeenAsked4Permission();

                        appData.checkPermissions.call(appData);    // Recursive call to check the answer given by the user

                    }
                }
            });
        }       
    }
szuzul commented 9 years ago

I totally agree with you but only in case of new app starting. For example, user uninstall app and after 3 months it again install. I expect that app ask me again should I accept the notifications not to save state of the app that was deleted. In my case i execute the window.plugin.notification.local.promptForPermission(); immediately after deviceready, and popup shows only on the first installation. After it doesn't show anymore. So behavior of the method promptForPermission() isn't correct cause it should open popup every time that is called but it doesn't.

MichelReij commented 9 years ago

Yup, I agree. But as far as I can read Objective C, the plug-in merely passes on the request unaltered to iOS, so the problem is probably with iOS:

/**
* Ask for permission to show badges.
*
* @param callback
* The function to be exec as the callback
*/
- (void) promptForPermission:(CDVInvokedUrlCommand *)command
{
if (IsAtLeastiOSVersion(@"8.0")) {
UIUserNotificationType types;
UIUserNotificationSettings *settings;
types = UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:nil];
[self.commandDelegate runInBackground:^{
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
}];
}
}
markwpearce commented 9 years ago

I agree - to me it looks like an iOS bug. If I remove the app from my phone, and re-install it, it should prompt for permissions, just like location services prompts for permission.

katzer commented 9 years ago

The new 0.8.x release is a complete rewrite. If the issue still exists please reopen it.

You may also take a look into the new wiki.

Thanks!

MikeBBarreiro commented 6 years ago

build.phonegap.com does not support de.appplant.cordova.plugin.local-notification to be built.

image