ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.04k stars 999 forks source link

Add requestPermissions() for push notifications / Local Notifications #848

Closed corysmc closed 6 years ago

corysmc commented 6 years ago

I'd like to be able to check if the user has granted permission for notifications. This way we tell them to change it in their settings, etc.

Proposed methods LocalNotifications.checkPermissions() or App.checkPermissions() (returns object of entire app permissions)

jcesarmobile commented 6 years ago

I went with LocalNotifications.areEnabled(), as it's not really a permission and the name is closer to the native one.

const areEnabled = await LocalNotifications.areEnabled();
console.log('are notifications enabled?', areEnabled.value);
Finesse commented 4 years ago

This method can be used to check if push notifications are enabled too. It would be really great if this fact is mentioned on https://capacitor.ionicframework.com/docs/apis/push-notifications because I've spend an hour to find a way to check push notifications permission.

mtpultz commented 3 years ago

@jcesarmobile what does requestPermissions() do for LocalNotifications? Seems like it doesn't do anything, but is a public API. I'm using areEnabled(), but find the existence of requestPermissions() confusing since it seems to only return an empty object literal in all cases.

export interface LocalNotificationsPlugin extends Plugin {
    schedule(options: {
        notifications: LocalNotification[];
    }): Promise<LocalNotificationScheduleResult>;
    getPending(): Promise<LocalNotificationPendingList>;
    registerActionTypes(options: {
        types: LocalNotificationActionType[];
    }): Promise<void>;
    cancel(pending: LocalNotificationPendingList): Promise<void>;
    areEnabled(): Promise<LocalNotificationEnabledResult>;
    createChannel(channel: NotificationChannel): Promise<void>;
    deleteChannel(channel: NotificationChannel): Promise<void>;
    listChannels(): Promise<NotificationChannelList>;
    requestPermission(): Promise<NotificationPermissionResponse>;
    addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotification) => void): PluginListenerHandle;
    addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: LocalNotificationActionPerformed) => void): PluginListenerHandle;
    /**
     * Remove all native listeners for this plugin
     */
    removeAllListeners(): void;
}
jcesarmobile commented 3 years ago

On Android all plugins have requestPermissions, but doesn't do anything on most of them, that's being removed on Capacitor 3 as, as you said, it's confusing. requestPermission on the other hand, it's for requesting notification permissions on iOS, used a different name since requestPermissions was an Android only thing. Will also probably change on v3, the push plugin is still not finished. Android doesn't need to request permissions for notifications, they just work.

mtpultz commented 3 years ago

Thanks @jcesarmobile, in Capacitor 3 would permissions be requested if the user turns off notifications manually? On Android they do work out of the box, but the user can turn them off so I create a custom Ionic modal indicating they are turned off.

jcesarmobile commented 3 years ago

No, on Android there are no permissions for notifications. If they are disabled, they are disabled, but there is no way for requesting the permission that doesn't exist, you'll have to continue prompting with a custom message to tell the users to enabled them.

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.